import apiClient from './apiClient';

// Types
export interface MaintenanceRecord {
  id: number;
  room_id: number;
  room_number?: string;
  maintenance_type: 'preventive' | 'corrective' | 'emergency' | 'upgrade' | 'inspection';
  status: 'scheduled' | 'in_progress' | 'completed' | 'cancelled' | 'on_hold';
  title: string;
  description?: string;
  scheduled_start: string;
  scheduled_end?: string;
  actual_start?: string;
  actual_end?: string;
  assigned_to?: number;
  assigned_staff_name?: string;
  priority: 'low' | 'medium' | 'high' | 'urgent';
  blocks_room: boolean;
  block_start?: string;
  block_end?: string;
  estimated_cost?: number;
  actual_cost?: number;
  notes?: string;
  created_at: string;
}

export interface HousekeepingTask {
  id: number;
  room_id: number;
  room_number?: string;
  booking_id?: number;
  task_type: 'checkout' | 'stayover' | 'vacant' | 'inspection' | 'turndown';
  status: 'pending' | 'in_progress' | 'completed' | 'skipped' | 'cancelled';
  scheduled_time: string;
  started_at?: string;
  completed_at?: string;
  assigned_to?: number;
  assigned_staff_name?: string;
  checklist_items?: ChecklistItem[];
  notes?: string;
  quality_score?: number;
  estimated_duration_minutes?: number;
  actual_duration_minutes?: number;
}

export interface ChecklistItem {
  item: string;
  completed: boolean;
  notes?: string;
}

export interface RoomInspection {
  id: number;
  room_id: number;
  room_number?: string;
  booking_id?: number;
  inspection_type: 'pre_checkin' | 'post_checkout' | 'routine' | 'maintenance' | 'damage';
  status: 'pending' | 'in_progress' | 'completed' | 'failed' | 'cancelled';
  scheduled_at: string;
  started_at?: string;
  completed_at?: string;
  inspected_by?: number;
  inspector_name?: string;
  checklist_items: InspectionChecklistItem[];
  overall_score?: number;
  overall_notes?: string;
  issues_found?: Issue[];
  requires_followup: boolean;
  created_at: string;
}

export interface InspectionChecklistItem {
  category: string;
  item: string;
  status: 'pass' | 'fail' | 'needs_attention' | 'not_applicable';
  notes?: string;
  photos?: string[];
}

export interface Issue {
  severity: 'critical' | 'major' | 'minor' | 'cosmetic';
  description: string;
  photo?: string;
}

export interface RoomStatusBoardItem {
  id: number;
  room_number: string;
  floor: number;
  status: 'available' | 'occupied' | 'maintenance' | 'cleaning';
  room_type?: string;
  current_booking?: {
    id: number;
    guest_name: string;
    check_out: string;
  };
  active_maintenance?: {
    id: number;
    title: string;
    type: string;
  };
  pending_housekeeping_count: number;
}

export interface OptimalRoomAssignmentRequest {
  room_type_id: number;
  check_in: string;
  check_out: string;
  num_guests: number;
  guest_preferences?: {
    view?: string;
    floor?: 'high' | 'low' | number;
    quiet?: boolean;
    accessible?: boolean;
  };
  exclude_room_ids?: number[];
}

// Service
const advancedRoomService = {
  // Room Assignment Optimization
  async assignOptimalRoom(request: OptimalRoomAssignmentRequest) {
    const response = await apiClient.post('/advanced-rooms/assign-optimal-room', request);
    return response.data;
  },

  async getRoomAvailabilityCalendar(roomId: number, startDate: string, endDate: string) {
    const response = await apiClient.get(
      `/advanced-rooms/${roomId}/availability-calendar`,
      { params: { start_date: startDate, end_date: endDate } }
    );
    return response.data;
  },

  // Maintenance Management
  async getMaintenanceRecords(params?: {
    room_id?: number;
    status?: string;
    maintenance_type?: string;
    page?: number;
    limit?: number;
  }) {
    const response = await apiClient.get('/advanced-rooms/maintenance', { params });
    return response.data;
  },

  async createMaintenanceRecord(data: {
    room_id: number;
    maintenance_type: string;
    title: string;
    description?: string;
    scheduled_start: string;
    scheduled_end?: string;
    assigned_to?: number;
    estimated_cost?: number;
    blocks_room?: boolean;
    block_start?: string;
    block_end?: string;
    priority?: string;
    notes?: string;
  }) {
    const response = await apiClient.post('/advanced-rooms/maintenance', data);
    return response.data;
  },

  async updateMaintenanceRecord(maintenanceId: number, data: {
    status?: string;
    actual_start?: string;
    actual_end?: string;
    completion_notes?: string;
    actual_cost?: number;
  }) {
    const response = await apiClient.put(`/advanced-rooms/maintenance/${maintenanceId}`, data);
    return response.data;
  },

  // Housekeeping Tasks
  async getHousekeepingTasks(params?: {
    room_id?: number;
    status?: string;
    task_type?: string;
    date?: string;
    page?: number;
    limit?: number;
  }) {
    const response = await apiClient.get('/advanced-rooms/housekeeping', { params });
    return response.data;
  },

  async createHousekeepingTask(data: {
    room_id: number;
    booking_id?: number;
    task_type: string;
    scheduled_time: string;
    assigned_to?: number;
    checklist_items?: ChecklistItem[];
    notes?: string;
    estimated_duration_minutes?: number;
  }) {
    const response = await apiClient.post('/advanced-rooms/housekeeping', data);
    return response.data;
  },

  async updateHousekeepingTask(taskId: number, data: {
    status?: string;
    checklist_items?: ChecklistItem[];
    notes?: string;
    issues_found?: string;
    quality_score?: number;
    inspected_by?: number;
    inspection_notes?: string;
  }) {
    const response = await apiClient.put(`/advanced-rooms/housekeeping/${taskId}`, data);
    return response.data;
  },

  // Room Inspections
  async getRoomInspections(params?: {
    room_id?: number;
    inspection_type?: string;
    status?: string;
    page?: number;
    limit?: number;
  }) {
    const response = await apiClient.get('/advanced-rooms/inspections', { params });
    return response.data;
  },

  async createRoomInspection(data: {
    room_id: number;
    booking_id?: number;
    inspection_type: string;
    scheduled_at: string;
    inspected_by?: number;
    checklist_items: InspectionChecklistItem[];
    checklist_template_id?: number;
  }) {
    const response = await apiClient.post('/advanced-rooms/inspections', data);
    return response.data;
  },

  async updateRoomInspection(inspectionId: number, data: {
    status?: string;
    checklist_items?: InspectionChecklistItem[];
    overall_score?: number;
    overall_notes?: string;
    issues_found?: Issue[];
    photos?: string[];
    requires_followup?: boolean;
    followup_notes?: string;
    maintenance_request_id?: number;
  }) {
    const response = await apiClient.put(`/advanced-rooms/inspections/${inspectionId}`, data);
    return response.data;
  },

  // Room Status Board
  async getRoomStatusBoard(floor?: number) {
    const response = await apiClient.get('/advanced-rooms/status-board', {
      params: floor ? { floor } : {}
    });
    return response.data;
  },
};

export default advancedRoomService;

