/**
 * Hook for managing date filter state
 */

import { useState, useCallback } from 'react';
import { DateRange } from '@/services/dashboard.service';
import { format } from 'date-fns';

export function useDateFilter(initialRange: DateRange = 'month') {
  const [dateRange, setDateRange] = useState<DateRange>(initialRange);
  const [customStartDate, setCustomStartDate] = useState<Date | undefined>(undefined);
  const [customEndDate, setCustomEndDate] = useState<Date | undefined>(undefined);
  const [showCustomDatePicker, setShowCustomDatePicker] = useState(false);
  const [tempCustomStartDate, setTempCustomStartDate] = useState<Date | undefined>(undefined);
  const [tempCustomEndDate, setTempCustomEndDate] = useState<Date | undefined>(undefined);

  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 {
      // Initialize temp state from actual state when opening
      setTempCustomStartDate(customStartDate);
      setTempCustomEndDate(customEndDate);
      setShowCustomDatePicker(true);
    }
  }, [customStartDate, customEndDate]);

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

  const handleCancelCustomDate = useCallback(() => {
    setShowCustomDatePicker(false);
    setTempCustomStartDate(customStartDate);
    setTempCustomEndDate(customEndDate);
    if (!customStartDate || !customEndDate) {
      setCustomStartDate(undefined);
      setCustomEndDate(undefined);
      setDateRange('month');
    }
  }, [customStartDate, customEndDate]);

  const handleStartDateChange = useCallback((date: Date | undefined) => {
    if (date) {
      setTempCustomStartDate(date);
      if (tempCustomEndDate && date > tempCustomEndDate) {
        setTempCustomEndDate(date);
      }
    } else {
      setTempCustomStartDate(undefined);
    }
  }, [tempCustomEndDate]);

  const handleEndDateChange = useCallback((date: Date | undefined) => {
    if (date) {
      setTempCustomEndDate(date);
      if (tempCustomStartDate && date < tempCustomStartDate) {
        setTempCustomStartDate(date);
      }
    } else {
      setTempCustomEndDate(undefined);
    }
  }, [tempCustomStartDate]);

  const getDisplayDateRange = useCallback((): string => {
    if (customStartDate && customEndDate) {
      return `${format(customStartDate, 'MMM d, yyyy')} - ${format(customEndDate, 'MMM d, yyyy')}`;
    }
    return 'Select dates';
  }, [customStartDate, customEndDate]);

  const reset = useCallback(() => {
    setDateRange('month');
    setCustomStartDate(undefined);
    setCustomEndDate(undefined);
    setShowCustomDatePicker(false);
    setTempCustomStartDate(undefined);
    setTempCustomEndDate(undefined);
  }, []);

  return {
    dateRange,
    customStartDate,
    customEndDate,
    showCustomDatePicker,
    tempCustomStartDate,
    tempCustomEndDate,
    setShowCustomDatePicker,
    handleDateRangeChange,
    handleApplyCustomDate,
    handleCancelCustomDate,
    handleStartDateChange,
    handleEndDateChange,
    getDisplayDateRange,
    reset,
  };
}

