import { defineStore } from 'pinia';
import { computed, ref } from 'vue';
import { withLoading } from 'src/utils/store-helpers';
import { timesheetApprovalService } from 'src/modules/timesheet-approval/services/timesheet-approval-service';
import { timesheetService } from 'src/modules/timesheets/services/timesheet-service';
import { default_pay_period_overview, type PayPeriodOverview } from "src/modules/timesheet-approval/models/pay-period-overview.models";
import { default_pay_period, type PayPeriod } from 'src/modules/shared/models/pay-period.models';
import { default_pay_period_details, type PayPeriodDetails } from 'src/modules/timesheets/models/pay-period-details.models';
import type { TimesheetApprovalCSVReportFilters } from 'src/modules/timesheet-approval/models/timesheet-approval-csv-report.models';

export const useTimesheetStore = defineStore('timesheet', () => {
    const is_loading = ref<boolean>(false);
    const pay_period = ref<PayPeriod>(default_pay_period);
    const pay_period_overviews = ref<PayPeriodOverview[]>([default_pay_period_overview,]);
    const current_pay_period_overview = ref<PayPeriodOverview>(default_pay_period_overview);
    const pay_period_details = ref<PayPeriodDetails>(default_pay_period_details);
    const pay_period_report = ref();
    const is_calendar_limit = computed(() =>
        pay_period.value.pay_year === 2024 &&
        pay_period.value.pay_period_no <= 1
    );

    const getPayPeriodByDateOrYearAndNumber = async (date_or_year: string | number, period_number?: number): Promise<boolean> => {
        is_loading.value = true;

        try {
            if (typeof date_or_year === 'string') {
                pay_period.value = await timesheetService.getPayPeriodByDate(date_or_year);
            }
            else if (typeof date_or_year === 'number' && period_number) {
                pay_period.value = await timesheetService.getPayPeriodByYearAndPeriodNumber(date_or_year, period_number);
            }
            else pay_period.value = default_pay_period;
            is_loading.value = false;

            return true;
        } catch (error) {
            console.error('Could not get current pay period: ', error);
            pay_period.value = default_pay_period;
            pay_period_overviews.value = [default_pay_period_overview,];
            //TODO: More in-depth error-handling here
            is_loading.value = false;

            return false;
        }
    };

    const getPayPeriodOverviewsBySupervisorEmail = async (pay_year: number, period_number: number, supervisor_email: string): Promise<boolean> => {
        is_loading.value = true;

        try {
            const response = await timesheetApprovalService.getPayPeriodOverviewsBySupervisorEmail(pay_year, period_number, supervisor_email);
            pay_period_overviews.value = response.employees_overview;
            is_loading.value = false;

            return true;
        } catch (error) {
            console.error('There was an error retrieving Employee Pay Period overviews: ', error);
            pay_period_overviews.value = [default_pay_period_overview,];
            // TODO: More in-depth error-handling here
            is_loading.value = false;
            
            return false;
        }
    };

    const getPayPeriodDetailsByEmployeeEmail = async (employee_email: string) => {
        is_loading.value = true;
        try {
            const response = await timesheetService.getPayPeriodDetailsByPayPeriodAndEmployeeEmail(
                pay_period.value.pay_year,
                pay_period.value.pay_period_no,
                employee_email
            );
            pay_period_details.value = response;
            console.log('pay period details: ', response, pay_period_details.value.employee_full_name)
            is_loading.value = false;
        } catch (error) {
            console.error('There was an error retrieving timesheet details for this employee: ', error);
            // TODO: More in-depth error-handling here
            pay_period_details.value = default_pay_period_details;
            is_loading.value = false;
        } 
    };

    const getPayPeriodReportByYearAndPeriodNumber = async (year: number, period_number: number, report_filters?: TimesheetApprovalCSVReportFilters) => {
        return withLoading(is_loading.value, async () => {
            try {
                const response = await timesheetApprovalService.getPayPeriodReportByYearAndPeriodNumber(
                    year,
                    period_number,
                    report_filters
                );
                pay_period_report.value = response;

                return true;
            } catch (error) {
                console.error('There was an error retrieving the report CSV: ', error);
                // TODO: More in-depth error-handling here
            }

            return false;
        });
    };

    return {
        is_loading,
        is_calendar_limit,
        pay_period,
        pay_period_overviews,
        current_pay_period_overview,
        pay_period_details,
        getPayPeriodByDateOrYearAndNumber,
        getPayPeriodOverviewsBySupervisorEmail,
        getPayPeriodDetailsByEmployeeEmail,
        getPayPeriodReportByYearAndPeriodNumber,
    };
});