
import React, { useState, useRef, useEffect, useMemo } from 'react';
import { SearchIcon } from './Icons';

interface MultiSelectDropdownProps {
  label: string;
  selected: string[];
  options: string[];
  onChange: (newSelected: string[]) => void;
  className?: string;
  enableRangeSelect?: boolean;
}

const MultiSelectDropdown: React.FC<MultiSelectDropdownProps> = ({ label, selected, options, onChange, className, enableRangeSelect }) => {
  const [isOpen, setIsOpen] = useState(false);
  const [searchTerm, setSearchTerm] = useState('');
  const [rangeInput, setRangeInput] = useState('');
  const dropdownRef = useRef<HTMLDivElement>(null);
  const inputRef = useRef<HTMLInputElement>(null);

  useEffect(() => {
    const handleClickOutside = (event: MouseEvent) => {
      if (dropdownRef.current && !dropdownRef.current.contains(event.target as Node)) {
        setIsOpen(false);
      }
    };
    document.addEventListener('mousedown', handleClickOutside);
    return () => document.removeEventListener('mousedown', handleClickOutside);
  }, []);

  // Focus input when opening
  useEffect(() => {
    if (isOpen && inputRef.current) {
      inputRef.current.focus();
    }
    if (!isOpen) {
      setSearchTerm(''); // Reset search when closing
    }
  }, [isOpen]);

  // Prevent body scroll when dropdown is open on mobile
  useEffect(() => {
    if (isOpen && window.innerWidth < 768) {
      document.body.style.overflow = 'hidden';
      return () => { document.body.style.overflow = ''; };
    }
  }, [isOpen]);

  const filteredOptions = useMemo(() => {
    if (!searchTerm) return options;
    return options.filter(opt => opt.toLowerCase().includes(searchTerm.toLowerCase()));
  }, [options, searchTerm]);

  const toggleOption = (option: string) => {
    if (selected.includes(option)) {
      onChange(selected.filter((item) => item !== option));
    } else {
      onChange([...selected, option]);
    }
  };

  const handleSelectAll = () => {
    // If searching, only select/deselect visible options
    if (searchTerm) {
      const allFilteredSelected = filteredOptions.every(opt => selected.includes(opt));
      if (allFilteredSelected) {
        // Deselect all filtered options
        onChange(selected.filter(item => !filteredOptions.includes(item)));
      } else {
        // Select all filtered options (add unique ones)
        const newSelected = Array.from(new Set([...selected, ...filteredOptions]));
        onChange(newSelected);
      }
    } else {
      // Standard behavior
      if (selected.length === options.length) {
        onChange([]); // Deselect all
      } else {
        onChange([...options]); // Select all
      }
    }
  };

  const handleClear = () => {
    onChange([]);
  };

  const handleRangeApply = () => {
    if (!rangeInput.trim()) return;

    const input = rangeInput.trim().toLowerCase().replace('w', ''); // Allow "W5" -> "5"
    let predicate: (num: number) => boolean = () => false;

    // Parse Input
    if (input.includes('-')) {
      const [start, end] = input.split('-').map(s => parseFloat(s.trim()));
      if (!isNaN(start) && !isNaN(end)) {
        predicate = (n) => n >= start && n <= end;
      }
    } else if (input.startsWith('<=')) {
      const val = parseFloat(input.substring(2).trim());
      if (!isNaN(val)) predicate = (n) => n <= val;
    } else if (input.startsWith('>=')) {
      const val = parseFloat(input.substring(2).trim());
      if (!isNaN(val)) predicate = (n) => n >= val;
    } else if (input.startsWith('<')) {
      const val = parseFloat(input.substring(1).trim());
      if (!isNaN(val)) predicate = (n) => n < val;
    } else if (input.startsWith('>')) {
      const val = parseFloat(input.substring(1).trim());
      if (!isNaN(val)) predicate = (n) => n > val;
    } else {
      // Exact match
      const val = parseFloat(input);
      if (!isNaN(val)) predicate = (n) => n === val;
    }

    // Apply to Options
    const newSelected = options.filter(opt => {
      // Extract number from option (e.g. "W1" -> 1, "2023" -> 2023)
      const num = parseFloat(opt.replace(/\D/g, ''));
      if (isNaN(num)) return false;
      return predicate(num);
    });

    onChange(newSelected);
    setRangeInput(''); // Clear input after apply
  };


  return (
    <div className={`flex flex-col min-w-[150px] relative ${className}`} ref={dropdownRef}>
      <label className="text-xs font-semibold text-slate-400 mb-1 uppercase tracking-wider">{label}</label>

      <button
        onClick={() => setIsOpen(!isOpen)}
        className={`w-full text-left bg-surface border border-border hover:border-slate-600 text-sm rounded-lg py-2 px-3 focus:outline-none focus:ring-2 focus:ring-primary/50 transition-colors flex justify-between items-center
        ${selected.length > 0 ? 'text-white font-medium border-primary/50' : 'text-slate-400'}`}
      >
        <span className="truncate">
          {selected.length === 0
            ? (label === 'Customer' ? 'All Customers' : `All ${label}s`)
            : `${selected.length} selected`}
        </span>
        <svg className={`fill-current h-4 w-4 transition-transform ${isOpen ? 'rotate-180' : ''}`} xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20">
          <path d="M9.293 12.95l.707.707L15.657 8l-1.414-1.414L10 10.828 5.757 6.586 4.343 8z" />
        </svg>
      </button>

      {/* Mobile overlay backdrop */}
      {isOpen && (
        <div className="fixed inset-0 bg-black/50 z-[99] md:hidden" onClick={() => setIsOpen(false)} />
      )}

      {isOpen && (
        <div className="fixed inset-x-3 bottom-3 max-h-[75vh] md:absolute md:inset-auto md:top-[calc(100%+4px)] md:left-0 md:bottom-auto md:w-64 md:max-h-96 overflow-hidden bg-slate-900 border border-slate-700 rounded-xl shadow-2xl z-[100] animate-fade-in flex flex-col">

          {/* Mobile header with close */}
          <div className="flex items-center justify-between p-3 border-b border-slate-800 md:hidden">
            <h3 className="text-sm font-bold text-white">{label}</h3>
            <button onClick={() => setIsOpen(false)} className="text-slate-400 active:text-white p-1">
              <svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" strokeWidth={2} stroke="currentColor" className="w-5 h-5">
                <path strokeLinecap="round" strokeLinejoin="round" d="M6 18 18 6M6 6l12 12" />
              </svg>
            </button>
          </div>

          {/* Search Bar */}
          <div className="p-2 border-b border-slate-800 sticky top-0 bg-slate-900 z-10">
            <div className="relative">
              <span className="absolute left-2.5 top-2.5 text-slate-500">
                <SearchIcon />
              </span>
              <input
                ref={inputRef}
                type="text"
                placeholder={`Search ${label}...`}
                className="w-full bg-slate-950 border border-slate-700 text-slate-200 text-sm rounded-md py-1.5 pl-9 pr-2 focus:outline-none focus:border-primary"
                value={searchTerm}
                onChange={(e) => setSearchTerm(e.target.value)}
                onKeyDown={(e) => {
                  if (e.key === 'Enter') {
                    e.preventDefault();
                    // Check if input contains multiple space-separated terms (bulk paste)
                    const terms = searchTerm.trim().split(/\s+/).filter(Boolean);
                    if (terms.length > 1) {
                      // Bulk select: match each term against options (case-insensitive)
                      const termsLower = terms.map(t => t.toLowerCase());
                      const matched = options.filter(opt =>
                        termsLower.some(t => opt.toLowerCase() === t || opt.toLowerCase().includes(t))
                      );
                      if (matched.length > 0) {
                        onChange(Array.from(new Set([...selected, ...matched])));
                      }
                      setIsOpen(false);
                      setSearchTerm('');
                    } else if (filteredOptions.length > 0) {
                      const allSelected = filteredOptions.every(opt => selected.includes(opt));
                      if (allSelected) {
                        // Deselect all filtered
                        onChange(selected.filter(item => !filteredOptions.includes(item)));
                      } else {
                        // Select all filtered
                        onChange(Array.from(new Set([...selected, ...filteredOptions])));
                      }
                      setIsOpen(false);
                      setSearchTerm('');
                    }
                  }
                }}
              />
            </div>
          </div>

          {/* Range Filter (Optional) */}
          {enableRangeSelect && (
            <div className="p-2 border-b border-slate-800 bg-slate-900/80 items-center flex gap-2">
              <input
                type="text"
                placeholder="Range (e.g. <=5, 1-5)"
                className="flex-1 bg-slate-950 border border-slate-700 text-slate-200 text-xs rounded px-2 py-1 focus:outline-none focus:border-primary"
                value={rangeInput}
                onChange={(e) => setRangeInput(e.target.value)}
                onKeyDown={(e) => e.key === 'Enter' && handleRangeApply()}
              />
              <button
                onClick={handleRangeApply}
                className="px-2 py-1 bg-indigo-600 hover:bg-indigo-500 text-white text-xs rounded"
              >
                Apply
              </button>
            </div>
          )}


          <div className="flex justify-between py-2 px-2 border-b border-slate-800 bg-slate-900/50">
            <button onClick={handleSelectAll} className="text-xs text-primary hover:text-indigo-400 font-medium px-2">
              {searchTerm
                ? (filteredOptions.every(opt => selected.includes(opt)) ? 'Unselect Results' : 'Select Results')
                : (selected.length === options.length ? 'Unselect All' : 'Select All')
              }
            </button>
            <button onClick={handleClear} className="text-xs text-slate-400 hover:text-white px-2">
              Clear
            </button>
          </div>

          <div className="space-y-1 overflow-y-auto custom-scrollbar scroll-touch p-2 max-h-60 md:max-h-60">
            {filteredOptions.map((opt) => (
              <label key={opt} className="flex items-center space-x-3 p-2.5 md:p-2 rounded hover:bg-slate-800 active:bg-slate-700 cursor-pointer group">
                <input
                  type="checkbox"
                  checked={selected.includes(opt)}
                  onChange={() => toggleOption(opt)}
                  className="form-checkbox h-5 w-5 md:h-4 md:w-4 text-primary rounded border-slate-600 bg-slate-800 focus:ring-primary focus:ring-offset-slate-900 transition duration-150 ease-in-out"
                />
                <span className={`text-sm break-all group-hover:text-white ${selected.includes(opt) ? 'text-white' : 'text-slate-400'}`}>
                  {opt}
                </span>
              </label>
            ))}
            {filteredOptions.length === 0 && <div className="p-4 text-center text-xs text-slate-500">No matches found</div>}
          </div>
        </div>
      )}
    </div>
  );
};

export default MultiSelectDropdown;
