import moment from "moment";
import { exportToExcel, exportToCSV, exportToPDF, printTable } from "../../utils/tableExportUtils";

/**
 * Handles export operations for employee data.
 * @param {string} type - Export type: 'csv', 'excel', 'pdf', or 'print'
 * @param {Array} employeeList - Full employee data array
 * @param {Array} filteredData - Filtered employee data (if search applied)
 * @param {string} searchText - Current search text (used to decide dataset)
 * @param {RefObject} tableRef - Table reference (used for print)
 */
const handleEmployeeExport = (type, employeeList, filteredData, searchText, tableRef) => {
    // Export full list (filtered if search applied)
    const dataToExport = searchText ? filteredData : employeeList;

    if (!dataToExport || dataToExport.length === 0) return;

    // Map and format employee data for export
    const exportData = dataToExport.map((item) => ({
        "First Name": item.firstName || "",
        "Middle Name": item.middleName || "",
        "Last Name": item.lastName || "",
        "Email": item.email || "",
        "Gender": item.gender || "",
        "Birth Date": item.birthdate
            ? moment(item.birthdate).format("DD-MMM-YYYY")
            : "",
        "Joining Date": item.joiningDate
            ? moment(item.joiningDate).format("DD-MMM-YYYY")
            : "",
        "Permanent Address": item.permanentAddress || "",
        "Current Address": item.currentAddress || "",
        "Phone Number": item.phoneNumber || "",
        "Emergency Phone Number": item.emergencyPhoneNumber || "",
        "Emergency Contact Person": item.emergencyContactPerson || "",
        "Is Active": item.isActive ? "Active" : "Inactive",
        "Job Role": item.jobRole || "",
    }));

    switch (type) {
        case "csv":
            exportToCSV(exportData, "employees");
            break;

        case "excel":
            exportToExcel(exportData, "employees");
            break;

        case "pdf":
            exportToPDF(
                dataToExport.map((item) => ({
                    Name: `${item.firstName || ""} ${item.lastName || ""}`.trim(),
                    Email: item.email || "",
                    "Phone Number": item.phoneNumber || "",
                    "Job Role": item.jobRole || "",
                    "Joining Date": item.joiningDate
                        ? moment(item.joiningDate).format("DD-MMM-YYYY")
                        : "",
                    Gender: item.gender || "",
                    Status: item.isActive ? "Active" : "Inactive",
                })),
                "employees",
                [
                    "Name",
                    "Email",
                    "Phone Number",
                    "Job Role",
                    "Joining Date",
                    "Gender",
                    "Status",
                ]
            );
            break;

        case "print":
            printTable(tableRef.current);
            break;

        default:
            break;
    }
};

export default handleEmployeeExport;
