import httpClient from '@/services/http';
import type {
  ZohoPeopleApiResponse,
  AttendanceReportResponse,
  EmployeeFormsResponse,
  HolidaysResponse,
  LeaveTrackerReportResponse,
  LeaveDataResponse,
} from '../types/zohoPeopleTypes';

const API_BASE_URL = '/api/v1/integrations/zoho/people';

export const zohoPeopleAPI = {
  // Get attendance report
  getAttendanceReport: async (): Promise<ZohoPeopleApiResponse<AttendanceReportResponse>> => {
    const response = await httpClient.get(`${API_BASE_URL}/attendance-report?provider=zoho`);
    console.log('attendance report response in zoho people api',response)
    return response.data as ZohoPeopleApiResponse<AttendanceReportResponse>;
  },

  // Get employee forms
  getEmployeeForms: async (page: number = 1, limit: number = 20): Promise<ZohoPeopleApiResponse<EmployeeFormsResponse>> => {
    const response = await httpClient.get(`${API_BASE_URL}/employee-forms?provider=zoho&page=${page}&limit=${limit}`);
    console.log('employee forms response in zoho people api',response)
    return response.data as ZohoPeopleApiResponse<EmployeeFormsResponse>;
  },

  // Get holidays
  getHolidays: async (): Promise<ZohoPeopleApiResponse<HolidaysResponse>> => {
    const response = await httpClient.get(`${API_BASE_URL}/holidays?provider=zoho`);
    console.log('holidays response in zoho people api',response)
    return response.data as ZohoPeopleApiResponse<HolidaysResponse>;
  },

  // Get leave tracker report
  getLeaveTrackerReport: async (): Promise<ZohoPeopleApiResponse<LeaveTrackerReportResponse>> => {
    const response = await httpClient.get(`${API_BASE_URL}/leave-tracker-report?provider=zoho`);
    console.log('leave tracker report response in zoho people api',response)
    return response.data as ZohoPeopleApiResponse<LeaveTrackerReportResponse>;
  },

  // Get leave data
  getLeaveData: async (): Promise<ZohoPeopleApiResponse<LeaveDataResponse>> => {
    const response = await httpClient.get(`${API_BASE_URL}/leave-data?provider=zoho`);
    console.log('leave data response in zoho people api',response)
    return response.data as ZohoPeopleApiResponse<LeaveDataResponse>;
  },

  // Get all dashboard data
  getAllDashboardData: async () => {
    try {
      const [attendanceReport, employeeForms, holidays, leaveTracker, leaveData] = await Promise.all([
        zohoPeopleAPI.getAttendanceReport(),
        zohoPeopleAPI.getEmployeeForms(),
        zohoPeopleAPI.getHolidays(),
        zohoPeopleAPI.getLeaveTrackerReport(),
        zohoPeopleAPI.getLeaveData(),
      ]);

      // Process attendance report
      const processedAttendanceReport = attendanceReport?.data?.data || [];

      // Process employee forms - handle the nested structure
      let processedEmployeeForms: any[] = [];
      if (employeeForms?.data?.data) {
        // The data is an array of objects where each object contains employee data
        processedEmployeeForms = employeeForms.data.data.flatMap((empGroup: any) => {
          return Object.values(empGroup).flat();
        });
      }

      // Process holidays
      const processedHolidays = holidays?.data?.data || [];

      // Process leave tracker - take first item from array
      const processedLeaveTracker = leaveTracker?.data?.data?.[0] || null;

      // Process leave data - handle the nested structure
      let processedLeaveData: any[] = [];
      if (leaveData?.data?.data) {
        processedLeaveData = leaveData.data.data.flatMap((leaveGroup: any) => {
          return Object.values(leaveGroup).flat();
        });
      }

      return {
        attendanceReport: processedAttendanceReport,
        employeeForms: processedEmployeeForms,
        holidays: processedHolidays,
        leaveTracker: processedLeaveTracker,
        leaveData: processedLeaveData,
      };
    } catch (error) {
      console.error('Error in getAllDashboardData:', error);
      throw error;
    }
  },
};
