import { useEmployeeStore } from "src/stores/employee-store";
import { useSchedulePresetsStore } from "src/stores/schedule-presets.store";
import { SchedulePreset } from "../models/schedule-presets.models";
import { isShiftOverlap } from "src/modules/timesheets/utils/shift.util";

export const useEmployeeListApi = () => {
    const employeeStore = useEmployeeStore();
    const schedulePresetStore = useSchedulePresetsStore();

    /**
     * Populates the employee store with the exhaustive list of all employees past and present
     * Also populates the schedule preset store with all schedule presets currently available.
     */
    const getEmployeeList = async (): Promise<void> => {
        employeeStore.is_loading = true;

        const success = await employeeStore.getEmployeeList();
        if (success) await schedulePresetStore.getSchedulePresetList();

        employeeStore.is_loading = false;
    };

    /**
     * Assigns the details of a specific employee to the employee store. If the employee has a 
     * schedule preset assigned, it also assign that preset to the schedule preset store. 
     * 
     * @param email email associated to employee.
     */
    const getEmployeeDetails = async (email: string): Promise<void> => {
        const success = await employeeStore.getEmployeeDetails(email);
        if (success && employeeStore.employee.preset_id !== null) {
            schedulePresetStore.setCurrentSchedulePreset(employeeStore.employee.preset_id ?? -1);
        }
    }

    /** 
     * Assigns the specified preset as the current preset in the schedule preset store and also
     * applies it to the current employee in the employee store. If a negative ID is provided, the
     * employee's assigned preset is set to null.
     * 
     * @param preset_id - the preset id currently selected
     */
    const setSchedulePreset = (preset_id: number) => {
        schedulePresetStore.setCurrentSchedulePreset(preset_id);
        employeeStore.employee.preset_id = preset_id < 0 ? null : preset_id;
    }

    /** 
     * Validates and converts the current schedule preset into a model that the backend 
     * can ingest and save, then sends a request to create or update preset.
     * 
     * @returns `true` if the preset is valid and was successfully saved, `false` otherwise.
     */
    const saveSchedulePreset = async (): Promise<boolean> => {
        const preset = schedulePresetStore.current_schedule_preset;
        preset.weekdays.forEach(weekday => weekday.is_error = isShiftOverlap(weekday.shifts));

        if (preset.weekdays.some(weekday => weekday.is_error)) {
            return false;
        }

        const preset_shifts = preset.weekdays.flatMap(weekday => weekday.shifts);
        const backend_preset = new SchedulePreset(
            preset.id,
            preset.name,
            preset_shifts
        );
        let success = false;

        if (preset.id === -1)
            success = await schedulePresetStore.createSchedulePreset(backend_preset);
        else
            success = await schedulePresetStore.updateSchedulePreset(backend_preset);

        if (success) {
            await schedulePresetStore.getSchedulePresetList();
            employeeStore.employee.preset_id = schedulePresetStore.current_schedule_preset.id;
        }

        return success;
    }

    /**
     * Sends request to delete the preset associated to the provided ID.
     * 
     * @param preset_id Backend ID of preset to delete
     * @return `true` if successfully deleted, `false` otherwise.
    */
    const deleteSchedulePreset = async (preset_id: number): Promise<boolean> => {
        const success = await schedulePresetStore.deleteSchedulePreset(preset_id);

        if (success)
            await schedulePresetStore.getSchedulePresetList();

        return success;
    }

    return {
        getEmployeeList,
        getEmployeeDetails,
        setSchedulePreset,
        saveSchedulePreset,
        deleteSchedulePreset,
    };
};