import http from '@/services/http';
import type { 
  CrmLead, 
  CrmTask, 
  CrmContact, 
  CrmDeal,
  CrmSearchParams,
  CrmApiResponse,
  CrmPaginatedResponse
} from '../types/CrmTypes';

// Available CRM resource types
export type CrmResourceType = 'leads' | 'tasks' | 'deals' | 'contacts' | 'sales-orders' | 'purchase-orders' | 'invoices';

// Base API endpoint
const CRM_BASE_URL = '/api/v1/integrations/data';

export const crmAPI = {
  // Generic method to get CRM data by resource type
  getCrmData: <T = any>(
    resource: CrmResourceType, 
    params?: CrmSearchParams
  ) => {
    const queryParams = {
      provider: 'zoho',
      service: 'crm',
      resource,
      page: 1,
      limit: 20,
      ...params
    };
    
    return http.get<CrmApiResponse<CrmPaginatedResponse<T>>>(CRM_BASE_URL, queryParams);
  },

  // Specific resource methods for type safety
  getLeads: (params?: CrmSearchParams) => 
    crmAPI.getCrmData<CrmLead>('leads', params),
  
  getTasks: (params?: CrmSearchParams) => 
    crmAPI.getCrmData<CrmTask>('tasks', params),
  
  getContacts: (params?: CrmSearchParams) => 
    crmAPI.getCrmData<CrmContact>('contacts', params),
  
  getDeals: (params?: CrmSearchParams) => 
    crmAPI.getCrmData<CrmDeal>('deals', params),
  
  // New API endpoints for sales orders, purchase orders, and invoices
  getSalesOrders: (params?: CrmSearchParams) => 
    http.get<CrmApiResponse<CrmPaginatedResponse<any>>>(`/api/v1/integrations/zoho/crm/sales-orders?provider=zoho`, params),
  
  getPurchaseOrders: (params?: CrmSearchParams) => 
    http.get<CrmApiResponse<CrmPaginatedResponse<any>>>(`/api/v1/integrations/zoho/crm/purchase-orders?provider=zoho`, params),
  
  getInvoices: (params?: CrmSearchParams) => 
    http.get<CrmApiResponse<CrmPaginatedResponse<any>>>(`/api/v1/integrations/zoho/crm/invoices?provider=zoho`, params),
};

