import httpClient from '@/services/http';
import type {
  ZohoBooksApiResponse,
  ZohoBooksContact,
  ZohoBooksInvoice,
  ZohoBooksExpense,
  ZohoBooksSalesOrder,
  ZohoBooksPurchaseOrder,
  ZohoBooksBill,
} from '../types/ZohoBooksTypes';

const BASE_URL = '/api/v1/integrations/zoho/books';

export const zohoBooksAPI = {
  // Get all contacts (customers and vendors)
  getContacts: async (): Promise<ZohoBooksApiResponse<ZohoBooksContact>> => {
    const response = await httpClient.get(`${BASE_URL}/contacts?provider=zoho`);
    console.log('contacts response in zoho books api',response)
    return response.data;
  },

  // Get vendors only
  getVendors: async (): Promise<ZohoBooksApiResponse<ZohoBooksContact>> => {
    const response = await httpClient.get(`${BASE_URL}/vendors?provider=zoho`);
    return response.data;
  },

  // Get customers only
  getCustomers: async (): Promise<ZohoBooksApiResponse<ZohoBooksContact>> => {
    const response = await httpClient.get(`${BASE_URL}/customers?provider=zoho`);
    return response.data;
  },

  // Get invoices
  getInvoices: async (): Promise<ZohoBooksApiResponse<ZohoBooksInvoice>> => {
    const response = await httpClient.get(`${BASE_URL}/invoices?provider=zoho`);
    return response.data;
  },

  // Get expenses
  getExpenses: async (): Promise<ZohoBooksApiResponse<ZohoBooksExpense>> => {
    const response = await httpClient.get(`${BASE_URL}/expenses?provider=zoho`);
    return response.data;
  },

  // Get sales orders
  getSalesOrders: async (): Promise<ZohoBooksApiResponse<ZohoBooksSalesOrder>> => {
    const response = await httpClient.get(`${BASE_URL}/sales-orders?provider=zoho`);
    return response.data;
  },

  // Get purchase orders
  getPurchaseOrders: async (): Promise<ZohoBooksApiResponse<ZohoBooksPurchaseOrder>> => {
    const response = await httpClient.get(`${BASE_URL}/purchase-orders?provider=zoho`);
    return response.data;
  },

  // Get bills
  getBills: async (): Promise<ZohoBooksApiResponse<ZohoBooksBill>> => {
    const response = await httpClient.get(`${BASE_URL}/bills?provider=zoho`);
    return response.data;
  },

  // Get all data for dashboard
  getAllData: async () => {
    try {
      const [
        contactsResponse,
        invoicesResponse,
        expensesResponse,
        salesOrdersResponse,
        purchaseOrdersResponse,
        billsResponse,
      ] = await Promise.all([
        zohoBooksAPI.getContacts(),
        zohoBooksAPI.getInvoices(),
        zohoBooksAPI.getExpenses(),
        zohoBooksAPI.getSalesOrders(),
        zohoBooksAPI.getPurchaseOrders(),
        zohoBooksAPI.getBills(),
      ]);

      return {
        contacts: contactsResponse.data.data,
        invoices: invoicesResponse.data.data,
        expenses: expensesResponse.data.data,
        salesOrders: salesOrdersResponse.data.data,
        purchaseOrders: purchaseOrdersResponse.data.data,
        bills: billsResponse.data.data,
      };
    } catch (error) {
      console.error('Error fetching Zoho Books data:', error);
      throw error;
    }
  },
};
