import { useShiftStore } from "src/stores/shift-store";
import { useTimesheetStore } from "src/stores/timesheet-store";

export const useShiftApi = () => {
    const timesheet_store = useTimesheetStore();
    const shift_store = useShiftStore();

    const deleteShiftById = async (shift_id: number, employee_email?: string) => {
        timesheet_store.is_loading = true;
        const success = await shift_store.deleteShiftById(shift_id, employee_email);

        if (success) {
            await timesheet_store.getTimesheetsByOptionalEmployeeEmail(employee_email);
        }

        timesheet_store.is_loading = false;
    };

    const saveShiftChanges = async (employee_email?: string) => {
        timesheet_store.is_loading = true;
        
        const update_success = await shift_store.updateShifts(employee_email);
        const create_success = await shift_store.createNewShifts(employee_email);

        if (create_success || update_success){
            await timesheet_store.getTimesheetsByOptionalEmployeeEmail(employee_email);
        }

        timesheet_store.is_loading = false;
    }

    return {
        deleteShiftById,
        saveShiftChanges,
    };
}