import api from '@/services/api';
import grOpsRoutes from '@/config/grOpsRoutes.json';

/**
 * Service para comunicação com o backend de Operações (Sistema de Operações)
 * Utiliza o arquivo de configuração grOpsRoutes.json para mapear os endpoints.
 */
export const grOpsService = {
  // Dashboard & Stats
  getDashboardStats: async () => {
    const route = grOpsRoutes.routes.dashboard.stats;
    const response = await api({
      method: route.method,
      url: route.path
    });
    return response.data;
  },

  // Automations (Outlook, Fiscal, CTe)
  processOutlook: async (data) => {
    const route = grOpsRoutes.routes.automations.outlook.process;
    const response = await api({
      method: route.method,
      url: route.path,
      data
    });
    return response.data;
  },

  getOutlookFilter: async () => {
    const route = grOpsRoutes.routes.automations.outlook.filter;
    const response = await api({
      method: route.method,
      url: route.path
    });
    return response.data;
  },

  // Management (Checklists, Solicitacoes, etc)
  getChecklists: async (params) => {
    const route = grOpsRoutes.routes.management.checklists;
    const response = await api({
      method: route.method,
      url: route.path,
      params
    });
    return response.data;
  },

  getSubmissions: async (params) => {
    const route = grOpsRoutes.routes.management.submissions;
    const response = await api({
      method: route.method,
      url: route.path,
      params
    });
    return response.data;
  },

  // Resources (Vehicles, Drivers)
  getVehicles: async () => {
    const route = grOpsRoutes.routes.fleet.vehicles;
    const response = await api({
      method: route.method,
      url: route.path
    });
    return response.data;
  },

  getDrivers: async () => {
    const route = grOpsRoutes.routes.fleet.drivers;
    const response = await api({
      method: route.method,
      url: route.path
    });
    return response.data;
  },

  // Generic Search helper based on grOpsRoutes
  fetchRoute: async (routePath, options = {}) => {
    const [category, subCategory, action] = routePath.split('.');
    const route = grOpsRoutes.routes[category]?.[subCategory]?.[action] || 
                  grOpsRoutes.routes[category]?.[subCategory];
    
    if (!route) throw new Error(`Route ${routePath} not found in grOpsRoutes.json`);

    return api({
      method: route.method,
      url: route.path,
      ...options
    });
  }
};
