import { useAuthStore } from "src/stores/auth-store";
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 auth_store = useAuthStore();

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

        if (success) {
            await timesheet_store.getTimesheetsByOptionalEmployeeEmail(auth_store.user?.email ?? '');
        }

        timesheet_store.is_loading = false;
    };

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

        if (create_success || update_success){
            await timesheet_store.getTimesheetsByOptionalEmployeeEmail(auth_store.user?.email ?? '');
        }

        timesheet_store.is_loading = false;
    }

    return {
        deleteShiftById,
        saveShiftChanges,
    };
}