import { ref } from "vue";
import { defineStore } from "pinia";
import { EmployeeListService } from "src/modules/employee-list/services/services-employee-list";
import { default_employee_profile, type EmployeeProfile } from "src/modules/employee-list/types/employee-profile-interface";
import type { EmployeeListTableItem } from "src/modules/employee-list/types/employee-list-table-interface";

export const useEmployeeStore = defineStore('employee', () => {  
    const employee = ref<EmployeeProfile>( default_employee_profile );
    const employeeList = ref<EmployeeListTableItem[]>([]);
    const isShowingEmployeeAddModifyWindow = ref<boolean>(false);
    const isLoadingEmployeeProfile = ref(false);
    const isLoadingEmployeeList = ref(false);

    const getEmployeeList = async () => {
        isLoadingEmployeeList.value = true;
        try {
            const response = await EmployeeListService.getEmployeeList();
            employeeList.value = response;
        } catch (error) {
            console.error("Ran into an error fetching employee list: ", error);
            //TODO: trigger an alert window with an error message here!
        }
        isLoadingEmployeeList.value = false;
    };

    const getEmployeeDetails = async (email: string) => {
        isLoadingEmployeeProfile.value = true;
        try {
            const response = await EmployeeListService.getEmployeeDetails(email);
            employee.value = response;

        } catch (error) {
            console.error('There was an error retrieving employee info: ', error);
            //TODO: trigger an alert window with an error message here!
        }
        isLoadingEmployeeProfile.value = false;
    };

    return { employee, employeeList, isShowingEmployeeAddModifyWindow, isLoadingEmployeeList, isLoadingEmployeeProfile, getEmployeeList, getEmployeeDetails };
});

