/**
 * Hook for managing My Requests filters and search using Redux
 */

import { useCallback, useEffect, useRef } from 'react';
import { useAppSelector, useAppDispatch } from '@/redux/hooks';
import { MyRequestsFilters } from '../types/myRequests.types';
import {
  setSearchTerm as setSearchTermAction,
  setStatusFilter as setStatusFilterAction,
  setPriorityFilter as setPriorityFilterAction,
  setTemplateTypeFilter as setTemplateTypeFilterAction,
  setCurrentPage as setCurrentPageAction,
  clearFilters as clearFiltersAction,
} from '../redux/myRequestsSlice';

interface UseMyRequestsFiltersOptions {
  onFiltersChange?: (filters: MyRequestsFilters) => void;
  debounceMs?: number;
}

export function useMyRequestsFilters({ onFiltersChange, debounceMs = 500 }: UseMyRequestsFiltersOptions = {}) {
  const dispatch = useAppDispatch();
  const debounceTimeoutRef = useRef<NodeJS.Timeout | null>(null);
  const isInitialMount = useRef(true);
  
  // Get filters from Redux
  const { searchTerm, statusFilter, priorityFilter, templateTypeFilter, currentPage } = useAppSelector((state) => state.myRequests);
  
  // Create setters that dispatch Redux actions
  const setSearchTerm = useCallback((value: string) => dispatch(setSearchTermAction(value)), [dispatch]);
  const setStatusFilter = useCallback((value: string) => dispatch(setStatusFilterAction(value)), [dispatch]);
  const setPriorityFilter = useCallback((value: string) => dispatch(setPriorityFilterAction(value)), [dispatch]);
  const setTemplateTypeFilter = useCallback((value: string) => dispatch(setTemplateTypeFilterAction(value)), [dispatch]);
  const setCurrentPage = useCallback((value: number) => dispatch(setCurrentPageAction(value)), [dispatch]);

  const getFilters = useCallback((): MyRequestsFilters => {
    return {
      search: searchTerm,
      status: statusFilter,
      priority: priorityFilter,
      templateType: templateTypeFilter,
    };
  }, [searchTerm, statusFilter, priorityFilter, templateTypeFilter]);

  // Debounced filter change handler
  useEffect(() => {
    // Skip initial mount - let component handle initial fetch
    if (isInitialMount.current) {
      isInitialMount.current = false;
      return;
    }
    
    if (debounceTimeoutRef.current) {
      clearTimeout(debounceTimeoutRef.current);
    }

    const timeoutId = setTimeout(() => {
      if (onFiltersChange) {
        onFiltersChange(getFilters());
      }
    }, searchTerm ? debounceMs : 0); // Debounce only for search, instant for dropdowns

    debounceTimeoutRef.current = timeoutId;

    return () => {
      if (debounceTimeoutRef.current) {
        clearTimeout(debounceTimeoutRef.current);
      }
    };
  }, [searchTerm, statusFilter, priorityFilter, templateTypeFilter, onFiltersChange, getFilters, debounceMs]);

  const resetFilters = useCallback(() => {
    dispatch(clearFiltersAction());
  }, [dispatch]);

  return {
    searchTerm,
    statusFilter,
    priorityFilter,
    templateTypeFilter,
    currentPage,
    setSearchTerm,
    setStatusFilter,
    setPriorityFilter,
    setTemplateTypeFilter,
    setCurrentPage,
    getFilters,
    resetFilters,
  };
}

