import { ref } from "vue";
import { defineStore } from "pinia";
import { SchedulePresetsService } from "src/modules/employee-list/services/schedule-presets-service";
import { type PresetManagerMode, SchedulePreset, SchedulePresetFrontend } from "src/modules/employee-list/models/schedule-presets.models";


export const useSchedulePresetsStore = defineStore('schedule_presets_store', () => {
    const schedule_presets = ref<SchedulePreset[]>([new SchedulePreset]);
    const current_schedule_preset = ref<SchedulePresetFrontend>(new SchedulePresetFrontend);
    const schedule_preset_dialog_mode = ref<PresetManagerMode | undefined>();
    const isManagerOpen = ref(false);

    /**
     * Opens the schedule preset manager with the preset associated with provided ID. If mode is 
     * set to `copy`, a clone of the preset will be created with a blank name instead.
     * @param preset_id 
     */
    const openSchedulePresetManager = (preset_id: number) => {
        if (preset_id === -1) {
            current_schedule_preset.value = new SchedulePresetFrontend;
        } else if (schedule_preset_dialog_mode.value === 'copy') {
            const preset = schedule_presets.value.find(preset => preset.id === preset_id)!;
            const copied_preset = new SchedulePresetFrontend(preset);
            copied_preset.id = -1;
            copied_preset.name = "";
            current_schedule_preset.value = copied_preset;
        } else 
            setCurrentSchedulePreset(preset_id);

        isManagerOpen.value = true;
    };
    
    const setCurrentSchedulePreset = (preset_id: number) => {
        if (preset_id === -1)
            current_schedule_preset.value = new SchedulePresetFrontend;
        else
            current_schedule_preset.value = new SchedulePresetFrontend(
                schedule_presets.value.find(preset => preset.id === preset_id)
            );
    };

    const createSchedulePreset = async (preset: SchedulePreset): Promise<boolean> => {
        try {
            const response = await SchedulePresetsService.createSchedulePresets(preset);
            
            if (response.success && response.data)
                current_schedule_preset.value = new SchedulePresetFrontend(response.data);

            return response.success;
        } catch (error) {
            console.error('DEV ERROR || error while creating schedule preset: ', error);

            return false;
        }
    };

    const updateSchedulePreset = async (preset: SchedulePreset): Promise<boolean> => {
        try {
            const response = await SchedulePresetsService.updateSchedulePresets(preset);

            return response.success;
        } catch (error) {
            console.error('DEV ERROR || error while updating schedule preset: ', error);

            return false;
        }

    };

    const deleteSchedulePreset = async (preset_id: number): Promise<boolean> => {
        try {
            const response = await SchedulePresetsService.deleteSchedulePresets(preset_id);

            return response.success;
        } catch (error) {
            console.error('DEV ERROR || error while deleting schedule preset: ', error);

            return false;
        }
    };

    const getSchedulePresetList = async (): Promise<boolean> => {
        try {
            const response = await SchedulePresetsService.getSchedulePresetsList();
            if  (response.success && response.data) 
                schedule_presets.value = response.data;
            
            return response.success;
        } catch (error) {
            console.error('DEV ERROR || error while searching for schedule presets: ', error);

            return false
        }
    };

    return {
        schedule_presets,
        current_schedule_preset,
        schedule_preset_dialog_mode,
        isManagerOpen,
        setCurrentSchedulePreset,
        openSchedulePresetManager,
        createSchedulePreset,
        updateSchedulePreset,
        deleteSchedulePreset,
        getSchedulePresetList,
    }
})