import { apiClient } from './apiClient';
import type { ApiResponse, Incident } from './types';

export const incidentsApi = {
  createIncident: async (incidentData: any, token: string): Promise<ApiResponse<Incident>> => {
    return apiClient.post('/v1/incidents', incidentData, { token });
  },

  getIncidents: async (params: { status?: string; limit?: number; date_from?: string }, token: string): Promise<ApiResponse<Incident[]>> => {
    const query = new URLSearchParams();
    if (params.status) query.append('status', params.status);
    if (params.limit) query.append('limit', params.limit.toString());
    if (params.date_from) query.append('date_from', params.date_from);

    return apiClient.get(`/v1/incidents?${query.toString()}`, { token });
  },

  getIncidentById: async (id: string, token: string): Promise<ApiResponse<Incident>> => {
    return apiClient.get(`/v1/incidents/${id}`, { token });
  },

  updateIncident: async (id: string, incidentData: any, token: string): Promise<ApiResponse<Incident>> => {
    return apiClient.patch(`/v1/incidents/${id}`, incidentData, { token });
  },

  getIncidentTimeline: async (id: string, token: string): Promise<ApiResponse<any[]>> => {
    return apiClient.get(`/v1/incidents/${id}/timeline`, { token });
  },

  getIncidentAudit: async (id: string, token: string): Promise<ApiResponse<any[]>> => {
    return apiClient.get(`/v1/incidents/${id}/audit`, { token });
  },

  recommendDispatch: async (payload: { gps_lat: number, gps_lon: number, vehicle_type_required: string }, token: string): Promise<ApiResponse<any>> => {
    return apiClient.post('/v1/dispatch/recommend', payload, { token });
  },

  dispatchVehicle: async (incidentId: string, vehicleId: string, token: string): Promise<ApiResponse<any>> => {
    return apiClient.post(`/v1/incidents/${incidentId}/dispatch`, { vehicle_id: vehicleId }, { token });
  },

  addPatientsToIncident: async (incidentId: string, patients: any[], token: string): Promise<ApiResponse<any>> => {
    return apiClient.post(`/v1/incidents/${incidentId}/patients`, { patients }, { token });
  }
};
