/**
 * Hook for managing dashboard export functionality
 */

import { useState, useCallback } from 'react';
import { DateRange } from '@/services/dashboard.service';
import { exportApproverPerformanceToCSV, exportDepartmentStatsToCSV } from '../utils/dashboardExports';

export function useDashboardExports() {
  const [exportingDeptStats, setExportingDeptStats] = useState(false);
  const [exportingApproverPerformance, setExportingApproverPerformance] = useState(false);

  const handleExportDepartmentStats = useCallback(async (
    dateRange: DateRange,
    customStartDate?: Date,
    customEndDate?: Date
  ) => {
    try {
      setExportingDeptStats(true);
      await exportDepartmentStatsToCSV(dateRange, customStartDate, customEndDate);
    } catch (error: any) {
      console.error('Failed to export department stats:', error);
      alert('Failed to export department statistics. Please try again.');
    } finally {
      setExportingDeptStats(false);
    }
  }, []);

  const handleExportApproverPerformance = useCallback(async (
    dateRange: DateRange,
    customStartDate?: Date,
    customEndDate?: Date
  ) => {
    try {
      setExportingApproverPerformance(true);
      await exportApproverPerformanceToCSV(dateRange, customStartDate, customEndDate);
    } catch (error: any) {
      console.error('Failed to export approver performance:', error);
      alert('Failed to export approver performance data. Please try again.');
    } finally {
      setExportingApproverPerformance(false);
    }
  }, []);

  return {
    exportingDeptStats,
    exportingApproverPerformance,
    handleExportDepartmentStats,
    handleExportApproverPerformance,
  };
}

