/* eslint-disable */
import { ref } from "vue";
import { defineStore } from "pinia";
import { EmployeeListService } from "src/modules/employee-list/services/employee-list-service";
import { EmployeeProfile } from "src/modules/employee-list/models/employee-profile.models";
import { Notify } from "quasar";

export const useEmployeeStore = defineStore('employee', () => {
    const employee = ref<EmployeeProfile>(new EmployeeProfile);
    const employee_list = ref<EmployeeProfile[]>([]);
    const is_add_modify_dialog_open = ref<boolean>(false);
    const management_mode = ref<'modify_employee' | 'add_employee'>('add_employee');
    const is_loading = ref(false);

    const openAddModifyDialog = async (employee_email?: string) => {

        if (employee_email === undefined) {
            management_mode.value = 'add_employee'
            employee.value = new EmployeeProfile();
            is_add_modify_dialog_open.value = true;
            return;
        }

        is_loading.value = true;
        management_mode.value = 'modify_employee';
        await getEmployeeDetails(employee_email);
        is_loading.value = false;
        is_add_modify_dialog_open.value = true;
    }

    const closeAddModifyDialog = () => {
        is_add_modify_dialog_open.value = false;
        management_mode.value = 'add_employee';
        employee.value = new EmployeeProfile;
    };

    const getEmployeeList = async (): Promise<boolean> => {
        try {
            const response = await EmployeeListService.getEmployeeList();
            if (response.success && response.data) employee_list.value = response.data;

            return response.success;
        } catch (error) {
            console.error("Ran into an error fetching employee list: ", error);
            return false;
        }
    };

    const getEmployeeDetails = async (email?: string): Promise<boolean> => {
        try {
            if (email === undefined) {
                const response = await EmployeeListService.getEmployeeDetails();
                if (response.success && response.data) employee.value = response.data;
                return response.success;
            } else {
                const response = await EmployeeListService.getEmployeeDetailsWithEmployeeEmail(email);
                if (response.success && response.data) employee.value = response.data;
                return response.success;
            }
        } catch (error) {
            console.error('There was an error retrieving employee info: ', error);
            //TODO: trigger an alert window with an error message here!
        }

        return false;
    };

    const createOrUpdateEmployee = async (profile: EmployeeProfile): Promise<boolean> => {
        let response;

        if (management_mode.value === 'add_employee') {
            const { birth_date, last_work_day, ...create_payload } = profile;
            response = await EmployeeListService.createNewEmployee(create_payload);
        } else {

            response = await EmployeeListService.updateEmployee(profile);
        }

        closeAddModifyDialog();

        if (response.success)
            await getEmployeeList();

        else
            Notify.create({
                message: 'failed to update or create employee',
                color: 'negative',
            });

        return response.success;
    };

    return {
        employee,
        employee_list,
        is_add_modify_dialog_open,
        management_mode,
        is_loading,
        getEmployeeList,
        getEmployeeDetails,
        openAddModifyDialog,
        closeAddModifyDialog,
        createOrUpdateEmployee,
    };
});

