import { Dialog, DialogContent, DialogHeader, DialogTitle } from "@/components/ui/dialog";
import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import { Checkbox } from "@/components/ui/checkbox";
import { useEffect, useMemo, useState } from "react";

export interface SelectItem {
  label: string;
  value: string;
}

interface SelectDialogProps {
  open: boolean;
  onClose: () => void;
  title: string;
  description?: string;
  icon?: React.ReactNode;
  items: SelectItem[];
  selectedValues?: string[];
  onConfirm: (values: string[]) => Promise<void> | void;
}

export function SelectDialog({
  open,
  onClose,
  title,
  description,
  icon,
  items,
  selectedValues,
  onConfirm,
}: SelectDialogProps) {
  const [selected, setSelected] = useState<string[]>([]);
  const [search, setSearch] = useState("");

  useEffect(() => {
    if (!open) return;
    if (!selectedValues) return;
    setSelected(selectedValues);
  }, [open, selectedValues]);

  const filteredItems = useMemo(() => {
    return items.filter((item) =>
      item.label.toLowerCase().includes(search.toLowerCase())
    );
  }, [items, search]);

  const toggleItem = (value: string) => {
    setSelected((prev) =>
      prev.includes(value) ? prev.filter((v) => v !== value) : [...prev, value]
    );
  };

  const handleConfirm = async () => {
    await onConfirm(selected);
    setSelected([]);
    setSearch("");
    onClose();
  };

  return (
    <Dialog open={open} onOpenChange={onClose}>
      <DialogContent className="max-w-md">
        <DialogHeader>
          <DialogTitle className="flex items-center gap-2">
            {icon}
            {title}
          </DialogTitle>
          {description && <p className="text-sm text-muted-foreground">{description}</p>}
        </DialogHeader>

        <Input
          placeholder="Tìm kiếm..."
          value={search}
          onChange={(e) => setSearch(e.target.value)}
          className="my-2"
        />

        <div className="max-h-64 overflow-y-auto space-y-2 mt-2 border rounded p-2">
          {filteredItems.map((item) => (
            <div key={item.value} className="flex items-center gap-2">
              <Checkbox
                checked={selected.includes(item.value)}
                onCheckedChange={() => toggleItem(item.value)}
              />
              <span>{item.label}</span>
            </div>
          ))}

          {filteredItems.length === 0 && (
            <p className="text-sm text-muted-foreground text-center">Không có kết quả</p>
          )}
        </div>

        <div className="flex justify-end gap-2 mt-4">
          <Button variant="outline" onClick={onClose}>
            Hủy
          </Button>
          <Button onClick={handleConfirm} disabled={selected.length === 0}>
            Xác nhận
          </Button>
        </div>
      </DialogContent>
    </Dialog>
  );
}
