import http from '@/services/http';
import type { 
  SalesforceEvent,
  SalesforceLead, 
  SalesforceTask, 
  SalesforceAccount, 
  SalesforceOpportunity,
  SalesforceSearchParams,
  SalesforceApiResponse
} from '../types/SalesforceCrmTypes';

// Available Salesforce CRM resource types
export type SalesforceResourceType = 'events' | 'leads' | 'tasks' | 'accounts' | 'opportunities';

// Base API endpoint for Salesforce CRM
const SALESFORCE_CRM_BASE_URL = '/api/v1/n8n/salesforce/crm';

export const salesforceCrmAPI = {
  // Generic method to get Salesforce CRM data by resource type
  getSalesforceCrmData: <T = any>(
    resource: SalesforceResourceType, 
    params?: SalesforceSearchParams
  ) => {
    const queryParams = {
      page: params?.page || 1,
      limit: params?.limit || 20,
      ...params
    };
    
    return http.get<SalesforceApiResponse<T>>(`${SALESFORCE_CRM_BASE_URL}/${resource}`, queryParams);
  },

  // Specific resource methods for type safety
  getEvents: (params?: SalesforceSearchParams) => 
    salesforceCrmAPI.getSalesforceCrmData<SalesforceEvent>('events', params),
  
  getLeads: (params?: SalesforceSearchParams) => 
    salesforceCrmAPI.getSalesforceCrmData<SalesforceLead>('leads', params),
  
  getTasks: (params?: SalesforceSearchParams) => 
    salesforceCrmAPI.getSalesforceCrmData<SalesforceTask>('tasks', params),
  
  getAccounts: (params?: SalesforceSearchParams) => 
    salesforceCrmAPI.getSalesforceCrmData<SalesforceAccount>('accounts', params),
  
  getOpportunities: (params?: SalesforceSearchParams) => 
    salesforceCrmAPI.getSalesforceCrmData<SalesforceOpportunity>('opportunities', params),

  // Get counts for all Salesforce CRM modules
  getSalesforceCrmCounts: () => {
    return http.get<{
      status: string;
      message: string;
      data: {
        events: { count: number; success: boolean };
        leads: { count: number; success: boolean };
        tasks: { count: number; success: boolean };
        accounts: { count: number; success: boolean };
        opportunities: { count: number; success: boolean };
      };
      timestamp: string;
    }>(`${SALESFORCE_CRM_BASE_URL}/counts`);
  },
};

