import { apiClient } from './apiClient';

export const fleetApi = {
  createStation: async (stationData: any, token: string) => {
    return apiClient.post('/v1/fleet/stations', stationData, { token });
  },

  updateStation: async (stationId: string, stationData: any, token: string) => {
    return apiClient.patch(`/v1/fleet/stations/${stationId}`, stationData, { token });
  },

  getStationDetails: async (stationId: string, token: string) => {
    return apiClient.get(`/v1/fleet/stations/${stationId}`, { token });
  },

  getStations: async (token: string, organisationId?: string) => {
    const url = organisationId 
      ? `/v1/fleet/stations?organisationId=${organisationId}`
      : `/v1/fleet/stations`;
      
    return apiClient.get(url, { token });
  },

  searchStationsByName: async (token: string, stationName: string) => {
    return apiClient.get(`/v1/fleet/stations?station_name=${encodeURIComponent(stationName)}`, { token });
  },

  getStationVehicles: async (token: string, stationId: string) => {
    return apiClient.get(`/v1/fleet/vehicles?station_id=${stationId}`, { token });
  },

  getVehiclesByStationName: async (token: string, stationName: string) => {
    return apiClient.get(`/v1/fleet/vehicles?station_name=${encodeURIComponent(stationName)}`, { token });
  },

  createVehicle: async (vehicleData: any, token: string) => {
    return apiClient.post('/v1/fleet/vehicles', vehicleData, { token });
  },

  getVehicles: async (token: string, orgId?: string) => {
    const url = orgId ? `/v1/fleet/vehicles?org_id=${orgId}` : `/v1/fleet/vehicles`;
    return apiClient.get(url, { token });
  },

  getVehiclesByRegistrationNumber: async (token: string, registrationNumber: string) => {
    return apiClient.get(`/v1/fleet/vehicles?registration_number=${encodeURIComponent(registrationNumber)}`, { token });
  },

  updateVehicleDetails: async (vehicleId: string, vehicleData: any, token: string) => {
    return apiClient.patch(`/v1/fleet/vehicles/${vehicleId}`, vehicleData, { token });
  },

  createStaff: async (staffData: any, token: string) => {
    return apiClient.post('/v1/fleet/staff', staffData, { token });
  },

  getStaff: async (token: string, orgId?: string) => {
    const url = orgId ? `/v1/fleet/staff?organisationId=${orgId}` : `/v1/fleet/staff`;
    return apiClient.get(url, { token });
  },

  searchStaff: async (token: string, params: { role?: string; name?: string }) => {
    const query = new URLSearchParams();
    if (params.role) query.append('role', params.role);
    if (params.name) query.append('name', params.name);
    const qs = query.toString();
    const url = qs ? `/v1/fleet/staff?${qs}` : `/v1/fleet/staff`;
    return apiClient.get(url, { token });
  },

  getStaffByRole: async (token: string, role: string) => {
    return apiClient.get(`/v1/fleet/staff?role=${encodeURIComponent(role)}`, { token });
  },

  getStaffByName: async (token: string, name: string) => {
    return apiClient.get(`/v1/fleet/staff?name=${encodeURIComponent(name)}`, { token });
  },

  getRoster: async (token: string) => {
    return apiClient.get('/v1/fleet/roster', { token });
  },

  createRoster: async (rosterData: any, token: string) => {
    return apiClient.post('/v1/fleet/roster', rosterData, { token });
  },

  startShift: async (shiftData: any, token: string) => {
    return apiClient.post('/v1/fleet/shifts/start', shiftData, { token });
  },

  getInventoryMaster: async (token: string) => {
    return apiClient.get('/v1/fleet/inventory/master', { token });
  },

  searchInventoryMaster: async (token: string, params: { name?: string; category?: string }) => {
    const query = new URLSearchParams();
    if (params.name) query.append('name', params.name);
    if (params.category) query.append('category', params.category);
    const qs = query.toString();
    const url = qs ? `/v1/fleet/inventory/master?${qs}` : `/v1/fleet/inventory/master`;
    return apiClient.get(url, { token });
  },

  createInventoryMaster: async (payload: any[], token: string) => {
    return apiClient.post('/v1/fleet/inventory/master', payload, { token });
  },

  updateInventoryMaster: async (itemId: string, payload: any, token: string) => {
    return apiClient.patch(`/v1/fleet/inventory/master/${itemId}`, payload, { token });
  },

  restockInventory: async (payload: any[], token: string) => {
    return apiClient.post('/v1/fleet/inventory/warehouse', payload, { token });
  },

  getInventoryMetadata: async (token: string, category?: string) => {
    const url = category ? `/v1/fleet/inventory/metadata?category=${category}` : '/v1/fleet/inventory/metadata';
    return apiClient.get(url, { token });
  },

  getWarehouseStock: async (token: string) => {
    return apiClient.get('/v1/fleet/inventory/warehouse', { token });
  },

  searchWarehouseStock: async (token: string, params: { name?: string; category?: string }) => {
    const query = new URLSearchParams();
    if (params.name) query.append('name', params.name);
    if (params.category) query.append('category', params.category);
    const qs = query.toString();
    const url = qs ? `/v1/fleet/inventory/warehouse?${qs}` : `/v1/fleet/inventory/warehouse`;
    return apiClient.get(url, { token });
  },

  assignToVehicle: async (vehicleId: string, payload: any, token: string) => {
    return apiClient.post(`/v1/fleet/vehicles/${vehicleId}/inventory/bulk`, payload, { token });
  },

  getPendingRestockRequests: async (token: string) => {
    return apiClient.get('/v1/fleet/inventory/restock-requests?status=PENDING', { token });
  },

  getRestockRequests: async (token: string, params?: { status?: string; search?: string }) => {
    const query = new URLSearchParams();
    if (params?.status) query.append('status', params.status);
    if (params?.search) query.append('search', params.search);
    const qs = query.toString();
    const url = qs ? `/v1/fleet/inventory/restock-requests?${qs}` : `/v1/fleet/inventory/restock-requests`;
    return apiClient.get(url, { token });
  },

  updateRestockRequestStatus: async (requestId: string, status: string, token: string) => {
    return apiClient.patch(`/v1/fleet/inventory/restock-requests/${requestId}/status`, { status }, { token });
  }
};
