"use client";

import React, { useState, useMemo } from "react";
import { Check, ChevronsUpDown } from "lucide-react";
import { Button } from "@/components/ui/button";
import {
  Command,
  CommandEmpty,
  CommandGroup,
  CommandInput,
  CommandItem,
  CommandList,
} from "@/components/ui/command";
import {
  Popover,
  PopoverContent,
  PopoverTrigger,
} from "@/components/ui/popover";
import { cn } from "@/lib/utils";
import type { TableInfo } from "../pop-dashboard/utils/dataFetcher";

interface TableComboboxProps {
  tables: TableInfo[];
  value: string;
  onSelect: (tableName: string) => void;
  placeholder?: string;
}

export function TableCombobox({
  tables,
  value,
  onSelect,
  placeholder = "테이블을 선택하세요",
}: TableComboboxProps) {
  const [open, setOpen] = useState(false);
  const [search, setSearch] = useState("");

  const selectedLabel = useMemo(() => {
    const found = tables.find((t) => t.table_name === value);
    return found ? (found.display_name || found.table_name) : "";
  }, [tables, value]);

  const filtered = useMemo(() => {
    if (!search) return tables;
    const q = search.toLowerCase();
    return tables.filter(
      (t) =>
        t.table_name.toLowerCase().includes(q) ||
        (t.display_name && t.display_name.toLowerCase().includes(q))
    );
  }, [tables, search]);

  return (
    <Popover open={open} onOpenChange={setOpen}>
      <PopoverTrigger asChild>
        <Button
          variant="outline"
          role="combobox"
          aria-expanded={open}
          className="mt-1 h-8 w-full justify-between text-xs"
        >
          {value ? selectedLabel : placeholder}
          <ChevronsUpDown className="ml-2 h-3.5 w-3.5 shrink-0 opacity-50" />
        </Button>
      </PopoverTrigger>
      <PopoverContent
        className="p-0"
        style={{ width: "var(--radix-popover-trigger-width)" }}
        align="start"
      >
        <Command shouldFilter={false}>
          <CommandInput
            placeholder="테이블명 또는 한글명 검색..."
            className="text-xs"
            value={search}
            onValueChange={setSearch}
          />
          <CommandList>
            <CommandEmpty className="py-4 text-center text-xs">
              검색 결과가 없습니다.
            </CommandEmpty>
            <CommandGroup>
              {filtered.map((table) => (
                <CommandItem
                  key={table.table_name}
                  value={table.table_name}
                  onSelect={() => {
                    onSelect(table.table_name);
                    setOpen(false);
                    setSearch("");
                  }}
                  className="text-xs"
                >
                  <Check
                    className={cn(
                      "mr-2 h-3.5 w-3.5",
                      value === table.table_name ? "opacity-100" : "opacity-0"
                    )}
                  />
                  <div className="flex flex-col">
                    <span>{table.display_name || table.table_name}</span>
                    {table.display_name && (
                      <span className="text-[10px] text-muted-foreground">
                        {table.table_name}
                      </span>
                    )}
                  </div>
                </CommandItem>
              ))}
            </CommandGroup>
          </CommandList>
        </Command>
      </PopoverContent>
    </Popover>
  );
}
