/**
 * Hook for managing Approver Performance filters
 */

import { useState, useCallback } from 'react';
import { useSearchParams } from 'react-router-dom';
import type { DateRange } from '@/services/dashboard.service';

export function useApproverPerformanceFilters() {
  const [searchParams] = useSearchParams();

  const [searchTerm, setSearchTerm] = useState('');
  const [statusFilter, setStatusFilter] = useState<string>(searchParams.get('status') || 'all');
  const [priorityFilter, setPriorityFilter] = useState<string>(searchParams.get('priority') || 'all');
  const [slaComplianceFilter, setSlaComplianceFilter] = useState<string>(searchParams.get('slaCompliance') || 'all');
  const [dateRange, setDateRange] = useState<DateRange>((searchParams.get('dateRange') as DateRange) || 'all');
  const [customStartDate, setCustomStartDate] = useState<Date | undefined>(
    searchParams.get('startDate') ? new Date(searchParams.get('startDate')!) : undefined
  );
  const [customEndDate, setCustomEndDate] = useState<Date | undefined>(
    searchParams.get('endDate') ? new Date(searchParams.get('endDate')!) : undefined
  );
  const [showCustomDatePicker, setShowCustomDatePicker] = useState(false);
  const [tempCustomStartDate, setTempCustomStartDate] = useState<Date | undefined>(undefined);
  const [tempCustomEndDate, setTempCustomEndDate] = useState<Date | undefined>(undefined);

  const clearFilters = useCallback(() => {
    setSearchTerm('');
    setStatusFilter('all');
    setPriorityFilter('all');
    setSlaComplianceFilter('all');
    setDateRange('all');
    setCustomStartDate(undefined);
    setCustomEndDate(undefined);
    setTempCustomStartDate(undefined);
    setTempCustomEndDate(undefined);
    setShowCustomDatePicker(false);
  }, []);

  const handleDateRangeChange = useCallback((value: string) => {
    const newRange = value as DateRange;
    setDateRange(newRange);
    if (newRange !== 'custom') {
      setCustomStartDate(undefined);
      setCustomEndDate(undefined);
      setTempCustomStartDate(undefined);
      setTempCustomEndDate(undefined);
      setShowCustomDatePicker(false);
    } else {
      setTempCustomStartDate(customStartDate);
      setTempCustomEndDate(customEndDate);
      setShowCustomDatePicker(true);
    }
  }, [customStartDate, customEndDate]);

  const handleApplyCustomDate = useCallback(() => {
    if (tempCustomStartDate && tempCustomEndDate) {
      if (tempCustomStartDate > tempCustomEndDate) {
        const temp = tempCustomStartDate;
        setCustomStartDate(tempCustomEndDate);
        setCustomEndDate(temp);
        setTempCustomStartDate(tempCustomEndDate);
        setTempCustomEndDate(temp);
      } else {
        setCustomStartDate(tempCustomStartDate);
        setCustomEndDate(tempCustomEndDate);
      }
      setShowCustomDatePicker(false);
    }
  }, [tempCustomStartDate, tempCustomEndDate]);

  return {
    // State
    searchTerm,
    statusFilter,
    priorityFilter,
    slaComplianceFilter,
    dateRange,
    customStartDate,
    customEndDate,
    showCustomDatePicker,
    tempCustomStartDate,
    tempCustomEndDate,
    // Setters
    setSearchTerm,
    setStatusFilter,
    setPriorityFilter,
    setSlaComplianceFilter,
    setDateRange,
    setCustomStartDate,
    setCustomEndDate,
    setShowCustomDatePicker,
    setTempCustomStartDate,
    setTempCustomEndDate,
    // Helpers
    clearFilters,
    handleDateRangeChange,
    handleApplyCustomDate
  };
}

