import ApiService from "./ApiService";
import type { AxiosResponse } from "axios";
import { downloadFile } from "@/lib/utils/downloadHelper";

// Export service for handling file downloads and exports
export class ExportService {
  /**
   * Export salary slip as PDF
   * @param payrollEntryId - The payroll entry ID
   * @returns Promise<Blob> - PDF file as blob
   */
  static async exportSalarySlipPdf(
    payrollEntryId: string | number
  ): Promise<AxiosResponse<Blob>> {
    return ApiService.request<Blob>({
      url: `/payroll/export/salary-slip/${payrollEntryId}/pdf`,
      method: "get",
      responseType: "blob",
    });
  }

  /**
   * Export payroll entries as Excel
   * @param payrollCycleId - The payroll cycle ID
   * @param filters - Optional filters to apply
   * @returns Promise<Blob> - Excel file as blob
   */
  static async exportPayrollEntries(
    payrollCycleId: string | number,
    filters?: Record<string, any>
  ): Promise<AxiosResponse<Blob>> {
    const params = { ...filters };

    return ApiService.request<Blob>({
      url: `/payroll/export/entries/${payrollCycleId}`,
      method: "get",
      responseType: "blob",
      params,
    });
  }

  /**
   * Export project costing report as Excel
   * @param filters - Filters to apply to the report
   * @returns Promise<Blob> - Excel file as blob
   */
  static async exportProjectCostingReport(
    filters?: Record<string, any>
  ): Promise<AxiosResponse<Blob>> {
    const params = { ...filters };

    return ApiService.request<Blob>({
      url: "reports/export/project",
      method: "get",
      responseType: "blob",
      params,
    });
  }

  /**
   * Export project costing list as Excel
   * @param filters - Filters to apply to the list
   * @returns Promise<Blob> - Excel file as blob
   */
  static async exportProjectCostingList(
    filters?: Record<string, any>
  ): Promise<AxiosResponse<Blob>> {
    const params = { ...filters };

    return ApiService.request<Blob>({
      url: "reports/export/project-costing-list",
      method: "get",
      responseType: "blob",
      params,
    });
  }

  /**
   * Export timesheet as Excel
   * @param filters - Filters to apply to the timesheet
   * @returns Promise<Blob> - Excel file as blob
   */
  static async exportTimesheet(
    filters?: Record<string, any>
  ): Promise<AxiosResponse<Blob>> {
    const params = { ...filters };

    return ApiService.request<Blob>({
      url: "reports/export/timesheet",
      method: "get",
      responseType: "blob",
      params,
    });
  }

  /**
   * Export clock-in report as Excel
   * @param filters - Filters to apply to the clock-in report
   * @returns Promise<Blob> - Excel file as blob
   */
  static async exportClockinReport(
    filters?: Record<string, any>
  ): Promise<AxiosResponse<Blob>> {
    const params = { ...filters };

    return ApiService.request<Blob>({
      url: "reports/export/clock-in",
      method: "get",
      responseType: "blob",
      params,
    });
  }

  /**
   * Helper function to generate filename with formatted timestamp
   * @param baseName - Base name for the file (e.g., "Timesheet report")
   * @returns Generated filename with formatted timestamp
   */
  static formatFileName(baseName: string): string {
    const now = new Date();
    const day = String(now.getDate()).padStart(2, "0");
    const month = now.toLocaleString("en-US", { month: "short" }); // Aug
    const year = now.getFullYear();
    let hours = now.getHours();
    const minutes = String(now.getMinutes()).padStart(2, "0");
    const ampm = hours >= 12 ? "PM" : "AM";
    hours = hours % 12 || 12;
    const timeStr = `${hours}_${minutes} ${ampm}`;
    return `${baseName} - #${day} ${month} ${year} ${timeStr}.xlsx`;
  }
}

export default ExportService;
