import api from '@/services/api';
import { handleRequest } from '@/services/serviceUtils';

// Endpoints definidos pelo usuário
const ENDPOINTS = {
  VEHICLES: '/cadastro_frota',
  MAINTENANCE: '/manutencao_frota',
  AVAILABILITY: '/disponibilidade_frota',
  MOKI: '/moki_frota',
  STATUS: '/status_frota',
  MONITORING: '/monitoramento_frota',
  CLAIMS: '/sinistro_devolucao_venda_frota',
  WORKSHOPS: '/oficinas_frota',
  AUTH: '/auth_monitoramento'
};

export const prafrotService = {
  // --- Autenticação ---
  login: (credentials) => handleRequest({
    apiFn: async () => {
      const { data } = await api.post(ENDPOINTS.AUTH, credentials);
      return data;
    }
  }),

  // --- Cadastro de Frota (Veículos) ---
  getVehicles: () => handleRequest({
    apiFn: async () => {
      const { data } = await api.get(`${ENDPOINTS.VEHICLES}/apresentar`);
      return data;
    }
  }),
  createVehicle: (payload) => handleRequest({
    apiFn: async () => {
      const { data } = await api.post(ENDPOINTS.VEHICLES, payload);
      return data;
    }
  }),
  updateVehicle: (id, payload) => handleRequest({
    apiFn: async () => {
      const { data } = await api.put(`${ENDPOINTS.VEHICLES}/${id}`, payload);
      return data;
    }
  }),

  // --- Manutenção ---
  getMaintenance: () => handleRequest({
    apiFn: async () => {
      const { data } = await api.get(`${ENDPOINTS.MAINTENANCE}/apresentar`);
      return data;
    }
  }),
  createMaintenance: (payload) => handleRequest({
    apiFn: async () => {
      // POST to base endpoint for creation
      const { data } = await api.post(ENDPOINTS.MAINTENANCE, payload);
      return data;
    }
  }),
  updateMaintenance: (id, payload) => handleRequest({
    apiFn: async () => {
      // PUT to base endpoint/{id} for update
      const { data } = await api.put(`${ENDPOINTS.MAINTENANCE}/${id}`, payload);
      return data;
    }
  }),

  // --- Disponibilidade ---
  getAvailability: () => handleRequest({
    apiFn: async () => {
      const { data } = await api.get(`${ENDPOINTS.AVAILABILITY}/apresentar`);
      return data;
    }
  }),
  createAvailability: (payload) => handleRequest({
    apiFn: async () => {
      const { data } = await api.post(ENDPOINTS.AVAILABILITY, payload);
      return data;
    }
  }),
  updateAvailability: (id, payload) => handleRequest({
    apiFn: async () => {
      const { data } = await api.put(`${ENDPOINTS.AVAILABILITY}/${id}`, payload);
      return data;
    }
  }),

  // --- Moki (Checklists) ---
  getMoki: () => handleRequest({
    apiFn: async () => {
      const { data } = await api.get(`${ENDPOINTS.MOKI}/apresentar`);
      return data;
    }
  }),
  createMoki: (payload) => handleRequest({
    apiFn: async () => {
      const { data } = await api.post(ENDPOINTS.MOKI, payload);
      return data;
    }
  }),
  updateMoki: (id, payload) => handleRequest({
    apiFn: async () => {
      const { data } = await api.put(`${ENDPOINTS.MOKI}/${id}`, payload);
      return data;
    }
  }),

  // --- Status Frota ---
  getStatus: () => handleRequest({
    apiFn: async () => {
      const { data } = await api.get(`${ENDPOINTS.STATUS}/apresentar`);
      return data;
    }
  }),
  createStatus: (payload) => handleRequest({
    apiFn: async () => {
      const { data } = await api.post(ENDPOINTS.STATUS, payload);
      return data;
    }
  }),
  updateStatus: (id, payload) => handleRequest({
    apiFn: async () => {
      const { data } = await api.put(`${ENDPOINTS.STATUS}/${id}`, payload);
      return data;
    }
  }),
  updateStatusBatch: (ids, statusFrota) => handleRequest({
    apiFn: async () => {
      const { data } = await api.put(`${ENDPOINTS.STATUS}/edit/em_lote`, { ids, status_frota: statusFrota });
      return data;
    }
  }),

  // --- Monitoramento ---
  getMonitoring: () => handleRequest({
    apiFn: async () => {
      const { data } = await api.get(`${ENDPOINTS.MONITORING}/apresentar`);
      return data;
    }
  }),
  createMonitoring: (payload) => handleRequest({
    apiFn: async () => {
      const { data } = await api.post(ENDPOINTS.MONITORING, payload);
      return data;
    }
  }),
  updateMonitoring: (id, payload) => handleRequest({
    apiFn: async () => {
      const { data } = await api.put(`${ENDPOINTS.MONITORING}/${id}`, payload);
      return data;
    }
  }),

  // --- Sinistro / Devolução ---
  getClaims: () => handleRequest({
    apiFn: async () => {
      const { data } = await api.get(`${ENDPOINTS.CLAIMS}/apresentar`);
      return data;
    }
  }),
  createClaims: (payload) => handleRequest({
    apiFn: async () => {
      const { data } = await api.post(ENDPOINTS.CLAIMS, payload);
      return data;
    }
  }),
  updateClaims: (id, payload) => handleRequest({
    apiFn: async () => {
      const { data } = await api.put(`${ENDPOINTS.CLAIMS}/${id}`, payload);
      return data;
    }
  }),

  // --- Oficinas ---
  getWorkshops: () => handleRequest({
    apiFn: async () => {
      const { data } = await api.get(`${ENDPOINTS.WORKSHOPS}/apresentar`);
      return data;
    }
  }),
  createWorkshop: (payload) => handleRequest({
    apiFn: async () => {
      const { data } = await api.post(ENDPOINTS.WORKSHOPS, payload);
      return data;
    }
  }),
  updateWorkshop: (id, payload) => handleRequest({
    apiFn: async () => {
      const { data } = await api.put(`${ENDPOINTS.WORKSHOPS}/${id}`, payload);
      return data;
    }
  }),

  // --- Motoristas (Drivers) ---
  getDrivers: () => handleRequest({
    apiFn: async () => {
      const { data } = await api.get('/motoristas/listagem');
      return data;
    }
  }),

  // --- Listas de Configuração (Config Lists) ---
  getListasConfig: () => handleRequest({
    apiFn: async () => {
      const { data } = await api.get('/listas_frota/config');
      return data;
    }
  }),

  getListasByRoute: (route) => handleRequest({
    apiFn: async () => {
      const { data } = await api.get(`/listas_frota/${route}`);
      return data;
    }
  }),

  // --- Update Vehicle Status (Inline Edit) ---
  updateVehicleStatus: (idVehicle, statusFrota) => handleRequest({
    apiFn: async () => {
      const { data } = await api.put(`/status_frota/edit/${idVehicle}`, {
        status_frota: statusFrota
      });
      return data;
    }
  }),

  // --- Update Status Batch (Bulk Edit) ---
  updateStatusBatch: (ids, statusFrota) => handleRequest({
    apiFn: async () => {
      const { data } = await api.put('/status_frota/edit/em_lote', {
        ids,
        status_frota: statusFrota
      });
      return data;
    }
  })
};
