import api from './api';
import apiRouteManager from './apiRouteManager';
import { handleRequest } from './serviceUtils';

/**
 * Prafrot Service V2 (Test Version)
 * Utiliza o ApiRouteManager para desacoplar as URLs e metadados.
 */
export const prafrotServiceV2 = {
  // --- Cadastro de Frota (Veículos) ---
  getVehicles: () => handleRequest({
    apiFn: async () => {
      const url = apiRouteManager.getUrl('prafrot.vehicles.list');
      const { data } = await api.get(url);
      return data;
    }
  }),

  createVehicle: (payload) => handleRequest({
    apiFn: async () => {
      const url = apiRouteManager.getUrl('prafrot.vehicles.create');
      const { data } = await api.post(url, payload);
      return data;
    }
  }),

  updateVehicle: (id, payload) => handleRequest({
    apiFn: async () => {
      const url = apiRouteManager.getUrl('prafrot.vehicles.update', { id });
      const { data } = await api.put(url, payload);
      return data;
    }
  }),

  deleteVehicle: (id) => handleRequest({
    apiFn: async () => {
      const url = apiRouteManager.getUrl('prafrot.vehicles.delete', { id });
      const { data } = await api.put(url);
      return data;
    }
  }),

  // --- Manutenção ---
  getMaintenance: () => handleRequest({
    apiFn: async () => {
      const url = apiRouteManager.getUrl('prafrot.maintenance.list');
      const { data } = await api.get(url);
      return data;
    }
  }),

  /**
   * Obtém os campos e metadados para formulários dinâmicos
   * @param {string} entity vehicles, maintenance, claims, etc.
   */
  getFields: (entity) => apiRouteManager.getFields(`prafrot.${entity}`),

  /**
   * Obtém metadados de apresentação (tabelas, cards, etc)
   * @param {string} entity vehicles, maintenance, etc.
   */
  getPresentation: (entity) => apiRouteManager.getPresentation(`prafrot.${entity}`)
};

export default prafrotServiceV2;
