/**
 * Service for managing guest complaints
 */
import apiClient from '../../../shared/services/apiClient';

export interface Complaint {
  id: number;
  title: string;
  description: string;
  category: string;
  priority: 'low' | 'medium' | 'high' | 'urgent';
  status: 'open' | 'in_progress' | 'resolved' | 'closed' | 'escalated';
  guest_id: number;
  booking_id?: number;
  room_id?: number;
  assigned_to?: number;
  resolution?: string;
  resolved_at?: string;
  resolved_by?: number;
  guest_satisfaction_rating?: number;
  guest_feedback?: string;
  internal_notes?: string;
  attachments?: string[];
  requires_follow_up: boolean;
  follow_up_date?: string;
  created_at: string;
  updated_at: string;
  updates?: ComplaintUpdate[];
}

export interface ComplaintUpdate {
  id: number;
  update_type: string;
  description: string;
  updated_by: number;
  created_at: string;
}

export interface CreateComplaintRequest {
  booking_id?: number;
  room_id?: number;
  category: string;
  priority?: string;
  title: string;
  description: string;
  attachments?: string[];
}

export interface UpdateComplaintRequest {
  status?: string;
  priority?: string;
  assigned_to?: number;
  resolution?: string;
  internal_notes?: string;
  requires_follow_up?: boolean;
  follow_up_date?: string;
}

export interface ResolveComplaintRequest {
  resolution: string;
  guest_satisfaction_rating?: number;
  guest_feedback?: string;
}

export interface AddComplaintUpdateRequest {
  update_type: string;
  description: string;
  metadata?: Record<string, unknown>;
}

export interface ComplaintFilters {
  status?: string;
  priority?: string;
  category?: string;
  assigned_to?: number;
  page?: number;
  limit?: number;
}

const complaintService = {
  /**
   * Create a new complaint
   */
  async createComplaint(data: CreateComplaintRequest) {
    const response = await apiClient.post('/complaints', data);
    return response.data;
  },

  /**
   * Get complaints with filters
   */
  async getComplaints(filters: ComplaintFilters = {}) {
    const params = new URLSearchParams();
    if (filters.status) params.append('status', filters.status);
    if (filters.priority) params.append('priority', filters.priority);
    if (filters.category) params.append('category', filters.category);
    if (filters.assigned_to) params.append('assigned_to', filters.assigned_to.toString());
    if (filters.page) params.append('page', filters.page.toString());
    if (filters.limit) params.append('limit', filters.limit.toString());

    const response = await apiClient.get(`/complaints?${params.toString()}`);
    return response.data;
  },

  /**
   * Get a specific complaint
   */
  async getComplaint(complaintId: number) {
    const response = await apiClient.get(`/complaints/${complaintId}`);
    return response.data;
  },

  /**
   * Update a complaint (admin/staff only)
   */
  async updateComplaint(complaintId: number, data: UpdateComplaintRequest) {
    const response = await apiClient.put(`/complaints/${complaintId}`, data);
    return response.data;
  },

  /**
   * Resolve a complaint (admin/staff only)
   */
  async resolveComplaint(complaintId: number, data: ResolveComplaintRequest) {
    const response = await apiClient.post(`/complaints/${complaintId}/resolve`, data);
    return response.data;
  },

  /**
   * Add an update to a complaint
   */
  async addComplaintUpdate(complaintId: number, data: AddComplaintUpdateRequest) {
    const response = await apiClient.post(`/complaints/${complaintId}/updates`, data);
    return response.data;
  },
};

export default complaintService;

