import apiClient from '../../../shared/services/apiClient';

export interface StaffShift {
  id: number;
  staff_id: number;
  staff_name?: string;
  shift_date: string;
  shift_type: 'morning' | 'afternoon' | 'night' | 'full_day' | 'custom';
  start_time: string;
  end_time: string;
  status: 'scheduled' | 'in_progress' | 'completed' | 'cancelled' | 'no_show';
  actual_start_time?: string;
  actual_end_time?: string;
  break_duration_minutes?: number;
  department?: string;
  notes?: string;
  handover_notes?: string;
  tasks_completed?: number;
  tasks_assigned?: number;
  assigned_by_name?: string;
}

export interface StaffTask {
  id: number;
  shift_id?: number;
  staff_id: number;
  staff_name?: string;
  title: string;
  description?: string;
  task_type: string;
  priority: 'low' | 'normal' | 'high' | 'urgent';
  status: 'pending' | 'assigned' | 'in_progress' | 'completed' | 'cancelled' | 'on_hold';
  scheduled_start?: string;
  scheduled_end?: string;
  actual_start?: string;
  actual_end?: string;
  estimated_duration_minutes?: number;
  actual_duration_minutes?: number;
  due_date?: string;
  related_booking_id?: number;
  related_room_id?: number;
  related_guest_request_id?: number;
  related_maintenance_id?: number;
  notes?: string;
  completion_notes?: string;
  assigned_by_name?: string;
}

const staffShiftService = {
  async getShifts(params?: {
    staff_id?: number;
    shift_date?: string;
    status?: string;
    department?: string;
    page?: number;
    limit?: number;
  }) {
    const response = await apiClient.get('/staff-shifts', { params });
    return response.data;
  },

  async getShift(shiftId: number) {
    const response = await apiClient.get(`/staff-shifts/${shiftId}`);
    return response.data;
  },

  async createShift(data: {
    staff_id?: number;
    shift_date: string;
    shift_type: string;
    start_time: string;
    end_time: string;
    status?: string;
    break_duration_minutes?: number;
    department?: string;
    notes?: string;
  }) {
    const response = await apiClient.post('/staff-shifts', data);
    return response.data;
  },

  async updateShift(shiftId: number, data: {
    shift_date?: string;
    shift_type?: string;
    start_time?: string;
    end_time?: string;
    status?: string;
    break_duration_minutes?: number;
    department?: string;
    notes?: string;
    handover_notes?: string;
  }) {
    const response = await apiClient.put(`/staff-shifts/${shiftId}`, data);
    return response.data;
  },

  async getTasks(params?: {
    staff_id?: number;
    shift_id?: number;
    status?: string;
    priority?: string;
    page?: number;
    limit?: number;
  }) {
    const response = await apiClient.get('/staff-shifts/tasks', { params });
    return response.data;
  },

  async createTask(data: {
    shift_id?: number;
    staff_id?: number;
    title: string;
    description?: string;
    task_type?: string;
    priority?: string;
    status?: string;
    scheduled_start?: string;
    scheduled_end?: string;
    estimated_duration_minutes?: number;
    due_date?: string;
    related_booking_id?: number;
    related_room_id?: number;
    related_guest_request_id?: number;
    related_maintenance_id?: number;
    notes?: string;
  }) {
    const response = await apiClient.post('/staff-shifts/tasks', data);
    return response.data;
  },

  async updateTask(taskId: number, data: {
    title?: string;
    description?: string;
    priority?: string;
    status?: string;
    completion_notes?: string;
    notes?: string;
  }) {
    const response = await apiClient.put(`/staff-shifts/tasks/${taskId}`, data);
    return response.data;
  },

  async getWorkloadSummary(date?: string) {
    const response = await apiClient.get('/staff-shifts/workload', { params: { date } });
    return response.data;
  },

  // Status change methods - only cancellation is manual, start/complete are automatic
  async cancelShift(shiftId: number, reason?: string, notes?: string) {
    const response = await apiClient.post(`/staff-shifts/${shiftId}/cancel`, {
      reason,
      notes,
    });
    return response.data;
  },

  async deleteShift(shiftId: number) {
    const response = await apiClient.delete(`/staff-shifts/${shiftId}`);
    return response.data;
  },
};

export default staffShiftService;

