import { api } from "src/boot/axios";
import type { UpsertShift } from "src/modules/timesheets/models/shift.models";
import type { PayPeriod } from "src/modules/shared/models/pay-period.models";
import type { PayPeriodDetails } from "src/modules/timesheets/models/pay-period-details.models";
import type { PayPeriodOverview } from "src/modules/timesheet-approval/models/pay-period-overview.models";
import type { Expense, PayPeriodExpenses, UpsertExpense } from "src/modules/timesheets/models/expense.models";

export const timesheetService = {
    getPayPeriodDetailsByEmployeeEmail: async (email: string): Promise<PayPeriodDetails> => {
        const response = await api.get(`/timesheets/${encodeURIComponent(email)}`);
        return response.data;
    },

    getPayPeriodByDate: async (date_string: string): Promise<PayPeriod> => {
        const response = await api.get(`pay-periods/date/${date_string}`);
        return response.data;
    },

    getPayPeriodByYearAndPeriodNumber: async (year: number, period_number: number): Promise<PayPeriod> => {
        const response = await api.get(`pay-periods/${year}/${period_number}`);
        return response.data;
    },

    getPayPeriodOverviewsBySupervisorEmail: async (year: number, period_number: number, supervisor_email: string): Promise<PayPeriodOverview[]> => {
        const response = await api.get(`pay-periods/${year}/${period_number}/${supervisor_email}`);
        return response.data;
    },

    getPayPeriodDetailsByPayPeriodAndEmployeeEmail: async (year: number, period_no: number, email: string): Promise<PayPeriodDetails> => {
        const response = await api.get('timesheets', { params: { year, period_no, email, } });
        return response.data;
    },

    getExpensesByPayPeriodAndEmployeeEmail: async (email: string, year: string, period_number: string): Promise<PayPeriodExpenses> => {
        const response = await api.get(`/expenses/${email}/${year}/${period_number}`);
        return response.data;
    },

    upsertOrDeleteShiftsByDateAndEmployeeEmail: async (email: string, payload: UpsertShift[], date: string): Promise<PayPeriodDetails> => {
        const response = await api.put(`/shifts/upsert/${email}/${date}`, payload);
        return response.data;
    },

    upsertOrDeleteExpensesByPayPeriodAndEmployeeEmail: async (email: string, date: string, payload: UpsertExpense): Promise<Expense[]> => {
        const headers = { 'Content-Type': 'application/json' }

        const response = await api.put(`/expenses/upsert/${email}/${date}`, payload, { headers });
        return response.data;
    }
};