"use client";

import React, { useState, useEffect, useCallback, useMemo } from "react";
import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle } from "@/components/ui/dialog";
import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs";
import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";
import { Badge } from "@/components/ui/badge";
import { Switch } from "@/components/ui/switch";
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select";
import { Accordion, AccordionContent, AccordionItem, AccordionTrigger } from "@/components/ui/accordion";
import { Popover, PopoverContent, PopoverTrigger } from "@/components/ui/popover";
import { Command, CommandEmpty, CommandGroup, CommandInput, CommandItem, CommandList } from "@/components/ui/command";
import { toast } from "sonner";
import { cn } from "@/lib/utils";
import {
  Database,
  Link2,
  Columns3,
  Key,
  Save,
  Plus,
  Pencil,
  Trash2,
  RefreshCw,
  Loader2,
  Check,
  ChevronsUpDown,
  Table2,
  ArrowRight,
  Eye,
  Settings2,
  Settings,
  Monitor,
  ExternalLink,
  Type,
  Hash,
  Calendar,
  ToggleLeft,
  FileText,
  Search,
  List,
  X,
} from "lucide-react";
import { getFieldJoins, createFieldJoin, updateFieldJoin, deleteFieldJoin, FieldJoin } from "@/lib/api/screenGroup";
import { tableManagementApi, ColumnTypeInfo, TableInfo, ColumnSettings } from "@/lib/api/tableManagement";
import { screenApi } from "@/lib/api/screen";
import { INPUT_TYPE_OPTIONS } from "@/types/input-types";
import TableManagementPage from "@/app/(main)/admin/systemMng/tableMngList/page";

// ============================================================
// 타입 정의
// ============================================================

interface JoinColumnRef {
  column: string;
  refTable: string;
  refTableLabel?: string;
  refColumn: string;
}

interface ReferencedBy {
  fromTable: string;
  fromTableLabel?: string;
  fromColumn: string;
  toColumn: string;
}

interface ColumnInfo {
  column: string;
  label?: string;
  type?: string;
  isPK?: boolean;
  isFK?: boolean;
  refTable?: string;
  refColumn?: string;
}

interface ScreenUsingTable {
  screenId: number;
  screenName: string;
  screenCode?: string;
  tableRole: string; // main, filter, join
}

interface TableSettingModalProps {
  isOpen: boolean;
  onClose: () => void;
  tableName: string;
  tableLabel?: string;
  screenId?: number;
  joinColumnRefs?: JoinColumnRef[];
  referencedBy?: ReferencedBy[];
  columns?: ColumnInfo[];
  filterColumns?: string[];
  onSaveSuccess?: () => void;
  isEmbedded?: boolean; // 탭 안에 임베드 모드로 표시
}

// 검색 가능한 Select 컴포넌트
interface SearchableSelectProps {
  value: string;
  onValueChange: (value: string) => void;
  options: Array<{ value: string; label: string; description?: string }>;
  placeholder?: string;
  disabled?: boolean;
  className?: string;
}

function SearchableSelect({
  value,
  onValueChange,
  options,
  placeholder = "선택...",
  disabled = false,
  className,
}: SearchableSelectProps) {
  const [open, setOpen] = useState(false);

  const selectedOption = options.find((opt) => opt.value === value);

  return (
    <Popover open={open} onOpenChange={setOpen}>
      <PopoverTrigger asChild>
        <Button
          variant="outline"
          role="combobox"
          aria-expanded={open}
          disabled={disabled}
          className={cn("h-8 w-full justify-between text-xs", className)}
        >
          {selectedOption ? (
            <span className="truncate">{selectedOption.label}</span>
          ) : (
            <span className="text-muted-foreground">{placeholder}</span>
          )}
          <ChevronsUpDown className="ml-2 h-4 w-4 shrink-0 opacity-50" />
        </Button>
      </PopoverTrigger>
      <PopoverContent className="p-0" style={{ width: "var(--radix-popover-trigger-width)" }} align="start">
        <Command>
          <CommandInput placeholder="검색..." className="text-xs" />
          <CommandList>
            <CommandEmpty className="py-2 text-center text-xs">결과 없음</CommandEmpty>
            <CommandGroup>
              {options.map((option) => (
                <CommandItem
                  key={option.value}
                  value={option.value}
                  onSelect={() => {
                    onValueChange(option.value);
                    setOpen(false);
                  }}
                  className="text-xs"
                >
                  <Check className={cn("mr-2 h-4 w-4", value === option.value ? "opacity-100" : "opacity-0")} />
                  <div className="flex flex-col">
                    <span>{option.label}</span>
                    {option.description && (
                      <span className="text-muted-foreground text-[10px]">{option.description}</span>
                    )}
                  </div>
                </CommandItem>
              ))}
            </CommandGroup>
          </CommandList>
        </Command>
      </PopoverContent>
    </Popover>
  );
}

// 입력 타입별 아이콘
function getInputTypeIcon(inputType: string) {
  switch (inputType) {
    case "text":
      return <Type className="h-3 w-3" />;
    case "number":
      return <Hash className="h-3 w-3" />;
    case "date":
    case "datetime":
      return <Calendar className="h-3 w-3" />;
    case "boolean":
    case "checkbox":
      return <ToggleLeft className="h-3 w-3" />;
    case "textarea":
      return <FileText className="h-3 w-3" />;
    case "entity":
      return <Link2 className="h-3 w-3" />;
    case "code":
      return <List className="h-3 w-3" />;
    default:
      return <Type className="h-3 w-3" />;
  }
}

// ============================================================
// 메인 모달 컴포넌트
// ============================================================

export function TableSettingModal({
  isOpen,
  onClose,
  tableName,
  tableLabel,
  screenId,
  joinColumnRefs = [],
  referencedBy = [],
  columns = [],
  filterColumns = [],
  onSaveSuccess,
  isEmbedded = false,
}: TableSettingModalProps) {
  const [activeTab, setActiveTab] = useState("columns");
  const [loading, setLoading] = useState(false);
  const [saving, setSaving] = useState(false);
  const [tableColumns, setTableColumns] = useState<ColumnTypeInfo[]>([]);
  const [tables, setTables] = useState<TableInfo[]>([]);
  const [fieldJoins, setFieldJoins] = useState<FieldJoin[]>([]);
  const [screensUsingTable, setScreensUsingTable] = useState<ScreenUsingTable[]>([]);

  // 선택된 컬럼
  const [selectedColumn, setSelectedColumn] = useState<string | null>(null);

  // 컬럼 설정 편집 상태
  const [editedColumns, setEditedColumns] = useState<Record<string, Partial<ColumnTypeInfo>>>({});

  // 테이블 라벨/설명
  const [editedTableLabel, setEditedTableLabel] = useState(tableLabel || "");
  const [editedTableDescription, setEditedTableDescription] = useState("");

  // 참조 테이블 컬럼 캐시
  const [refTableColumns, setRefTableColumns] = useState<Record<string, ColumnTypeInfo[]>>({});
  const [loadingRefColumns, setLoadingRefColumns] = useState(false);

  // 테이블 타입 관리 모달 상태
  const [showTableManagementModal, setShowTableManagementModal] = useState(false);

  // 테이블 컬럼 정보 로드
  const loadTableData = useCallback(async () => {
    if (!tableName) return;

    setLoading(true);
    try {
      // 테이블 목록 로드
      const tablesResponse = await tableManagementApi.getTableList();
      if (tablesResponse.success && tablesResponse.data) {
        setTables(tablesResponse.data);
      }

      // 테이블 컬럼 로드 (column_labels 정보 포함)
      const columnsResponse = await tableManagementApi.getColumnList(tableName);
      if (columnsResponse.success && columnsResponse.data?.columns) {
        // 백엔드 응답은 camelCase로 옴
        const columnsData = columnsResponse.data.columns;
        setTableColumns(columnsData);

        // 초기 편집 상태 설정
        const initialEdits: Record<string, Partial<ColumnTypeInfo>> = {};
        columnsData.forEach((col) => {
          // referenceTable이 설정되어 있으면 inputType은 entity여야 함
          let effectiveInputType = col.inputType || "direct";
          if (col.referenceTable && effectiveInputType !== "entity") {
            effectiveInputType = "entity";
          }
          // codeCategory/codeValue가 설정되어 있으면 inputType은 code여야 함
          if (col.codeCategory && effectiveInputType !== "code") {
            effectiveInputType = "code";
          }

          initialEdits[col.columnName] = {
            displayName: col.displayName,
            inputType: effectiveInputType,
            referenceTable: col.referenceTable,
            referenceColumn: col.referenceColumn,
            displayColumn: col.displayColumn,
          };
        });
        setEditedColumns(initialEdits);
      }

      // 필드 조인 로드 (screenId가 있는 경우)
      if (screenId) {
        const joinsResponse = await getFieldJoins(screenId);
        if (joinsResponse.success && joinsResponse.data) {
          const relevantJoins = joinsResponse.data.filter(
            (j) => j.save_table === tableName || j.join_table === tableName,
          );
          setFieldJoins(relevantJoins);
        }
      }

      // 이 테이블을 사용하는 화면 목록 로드
      await loadScreensUsingTable();
    } catch (error) {
      console.error("테이블 정보 로드 실패:", error);
      toast.error("테이블 정보를 불러오는데 실패했습니다.");
    } finally {
      setLoading(false);
    }
  }, [tableName, screenId]);

  // 이 테이블을 사용하는 화면 목록 로드
  const loadScreensUsingTable = useCallback(async () => {
    if (!tableName) return;

    try {
      // 모든 화면 조회
      const screensResponse = await screenApi.getScreens({ size: 1000 });
      if (screensResponse.data) {
        const usingScreens: ScreenUsingTable[] = [];

        screensResponse.data.forEach((screen: any) => {
          // 메인 테이블로 사용하는 경우
          if (screen.tableName === tableName) {
            usingScreens.push({
              screenId: screen.screenId,
              screenName: screen.screenName,
              screenCode: screen.screenCode,
              tableRole: "main",
            });
          }
          // TODO: 필터 테이블, 조인 테이블로 사용하는 경우도 추가
        });

        setScreensUsingTable(usingScreens);
      }
    } catch (error) {
      console.error("화면 목록 로드 실패:", error);
    }
  }, [tableName]);

  // 참조 테이블 컬럼 로드
  const loadRefTableColumns = useCallback(
    async (refTableName: string) => {
      if (!refTableName || refTableName === "none" || refTableColumns[refTableName]) return;

      setLoadingRefColumns(true);
      try {
        const response = await tableManagementApi.getColumnList(refTableName);
        if (response.success && response.data?.columns) {
          setRefTableColumns((prev) => ({
            ...prev,
            [refTableName]: response.data!.columns,
          }));
        }
      } catch (error) {
        console.error("참조 테이블 컬럼 로드 실패:", error);
      } finally {
        setLoadingRefColumns(false);
      }
    },
    [refTableColumns],
  );

  useEffect(() => {
    if (isOpen && tableName) {
      loadTableData();
      setEditedTableLabel(tableLabel || tableName);
    }
  }, [isOpen, tableName, tableLabel, loadTableData]);

  // 참조 테이블 변경 시 컬럼 로드
  useEffect(() => {
    Object.values(editedColumns).forEach((col) => {
      if (col.referenceTable && col.referenceTable !== "none") {
        loadRefTableColumns(col.referenceTable);
      }
    });
  }, [editedColumns, loadRefTableColumns]);

  // 새로고침
  const handleRefresh = () => {
    loadTableData();
    toast.success("새로고침 완료");
  };

  // 컬럼 설정 변경 핸들러
  const handleColumnChange = (columnName: string, field: string, value: any) => {
    setEditedColumns((prev) => ({
      ...prev,
      [columnName]: {
        ...prev[columnName],
        [field]: value,
      },
    }));

    // 입력 타입 변경 시 관련 필드 초기화
    if (field === "inputType") {
      // 엔티티가 아닌 다른 타입으로 변경하면 참조 설정 초기화
      if (value !== "entity") {
        setEditedColumns((prev) => ({
          ...prev,
          [columnName]: {
            ...prev[columnName],
            inputType: value,
            referenceTable: "",
            referenceColumn: "",
            displayColumn: "",
          },
        }));
      }
      // 코드가 아닌 다른 타입으로 변경하면 코드 설정 초기화
      if (value !== "code") {
        setEditedColumns((prev) => ({
          ...prev,
          [columnName]: {
            ...prev[columnName],
            inputType: value,
            codeCategory: "",
            codeValue: "",
          },
        }));
      }
    }

    // 참조 테이블 변경 시 참조 컬럼 초기화
    if (field === "referenceTable") {
      setEditedColumns((prev) => ({
        ...prev,
        [columnName]: {
          ...prev[columnName],
          referenceColumn: "",
          displayColumn: "",
        },
      }));
      if (value && value !== "none") {
        loadRefTableColumns(value);
      }
    }
  };

  // 전체 저장 (테이블타입관리 페이지와 동일한 로직)
  const handleSaveAll = async () => {
    setSaving(true);
    try {
      // 변경된 컬럼들만 저장
      for (const [columnName, editedSettings] of Object.entries(editedColumns)) {
        // 기존 컬럼 정보 찾기
        const originalColumn = tableColumns.find((c) => c.columnName === columnName);
        if (!originalColumn) continue;

        // 기존 값과 편집된 값 병합
        const mergedColumn = {
          ...originalColumn,
          ...editedSettings,
        };

        // detailSettings 처리 (Entity 타입인 경우)
        let finalDetailSettings = mergedColumn.detailSettings || "";

        // referenceTable이 설정되어 있으면 inputType을 entity로 자동 설정
        let currentInputType = (mergedColumn.inputType || "") as string;
        if (mergedColumn.referenceTable && currentInputType !== "entity") {
          currentInputType = "entity";
        }
        // codeCategory가 설정되어 있으면 inputType을 code로 자동 설정
        if (mergedColumn.codeCategory && currentInputType !== "code") {
          currentInputType = "code";
        }

        if (currentInputType === "entity" && mergedColumn.referenceTable) {
          // 기존 detailSettings를 파싱하거나 새로 생성
          let existingSettings: Record<string, unknown> = {};
          if (typeof mergedColumn.detailSettings === "string" && mergedColumn.detailSettings.trim().startsWith("{")) {
            try {
              existingSettings = JSON.parse(mergedColumn.detailSettings);
            } catch {
              existingSettings = {};
            }
          }

          // 엔티티 설정 추가
          const entitySettings = {
            ...existingSettings,
            entityTable: mergedColumn.referenceTable,
            entityCodeColumn: mergedColumn.referenceColumn || "id",
            entityLabelColumn: mergedColumn.displayColumn || "name",
            placeholder: (existingSettings.placeholder as string) || "항목을 선택하세요",
            searchable: existingSettings.searchable ?? true,
          };

          finalDetailSettings = JSON.stringify(entitySettings);
          console.log("Entity 설정 JSON 생성:", entitySettings);
        }

        // Code 타입인 경우 hierarchyRole을 detailSettings에 포함
        if (currentInputType === "code" && (mergedColumn as any).hierarchyRole) {
          let existingSettings: Record<string, unknown> = {};
          if (typeof finalDetailSettings === "string" && finalDetailSettings.trim().startsWith("{")) {
            try {
              existingSettings = JSON.parse(finalDetailSettings);
            } catch {
              existingSettings = {};
            }
          }

          const codeSettings = {
            ...existingSettings,
            hierarchyRole: (mergedColumn as any).hierarchyRole,
          };

          finalDetailSettings = JSON.stringify(codeSettings);
          console.log("Code 계층 역할 설정 JSON 생성:", codeSettings);
        }

        // ColumnSettings 인터페이스에 맞게 데이터 구성
        const columnSetting: ColumnSettings = {
          columnName: columnName,
          columnLabel: mergedColumn.displayName || originalColumn.displayName || "",
          inputType: currentInputType || "text", // referenceTable/codeCategory가 설정된 경우 자동 보정된 값 사용
          detailSettings: finalDetailSettings,
          codeCategory: mergedColumn.codeCategory || originalColumn.codeCategory || "",
          codeValue: mergedColumn.codeValue || originalColumn.codeValue || "",
          referenceTable: mergedColumn.referenceTable || "",
          referenceColumn: mergedColumn.referenceColumn || "",
          displayColumn: mergedColumn.displayColumn || "",
        };

        console.log("저장할 컬럼 설정:", columnSetting);

        // API 호출
        const response = await tableManagementApi.updateColumnSettings(tableName, columnName, columnSetting);

        if (!response.success) {
          console.error(`컬럼 '${columnName}' 저장 실패:`, response.message);
          toast.error(`컬럼 '${columnName}' 저장 실패: ${response.message}`);
          return;
        }
      }

      toast.success("테이블 설정이 저장되었습니다.");
      setEditedColumns({}); // 편집 상태 초기화
      onSaveSuccess?.();
      await loadTableData();
    } catch (error) {
      console.error("저장 실패:", error);
      toast.error("저장에 실패했습니다.");
    } finally {
      setSaving(false);
    }
  };

  // 컬럼 정보 통합
  const mergedColumns = useMemo(() => {
    const columnsMap = new Map<string, ColumnTypeInfo & { isPK?: boolean; isFK?: boolean }>();

    // API에서 가져온 컬럼 정보 (camelCase)
    tableColumns.forEach((tcol) => {
      columnsMap.set(tcol.columnName, {
        ...tcol,
        isPK: tcol.isPrimaryKey,
        isFK: false, // 백엔드에서 isForeignKey를 제공하지 않으므로 false 기본값
      });
    });

    return Array.from(columnsMap.values());
  }, [tableColumns]);

  // 선택된 컬럼 정보
  const selectedColumnInfo = useMemo(() => {
    if (!selectedColumn) return null;
    return mergedColumns.find((c) => c.columnName === selectedColumn);
  }, [selectedColumn, mergedColumns]);

  // 테이블 옵션
  const tableOptions = useMemo(
    () => [
      { value: "none", label: "-- 선택 안함 --" },
      ...tables.map((t) => ({
        value: t.tableName,
        label: t.displayName || t.tableName,
        description: t.tableName,
      })),
    ],
    [tables],
  );

  // 입력 타입 옵션
  const inputTypeOptions = useMemo(
    () =>
      INPUT_TYPE_OPTIONS.map((opt) => ({
        value: opt.value,
        label: opt.label,
      })),
    [],
  );

  // 참조 테이블 컬럼 옵션
  const getRefColumnOptions = (refTable: string) => {
    const cols = refTableColumns[refTable] || [];
    return [
      { value: "", label: "-- 선택 안함 --" },
      ...cols.map((c) => ({
        value: c.columnName,
        label: c.displayName || c.columnName,
        description: c.dataType,
      })),
    ];
  };

  // 임베드 모드
  if (isEmbedded) {
    return (
      <>
        <div className="flex h-full flex-col">
          {/* 헤더 */}
          <div className="flex flex-shrink-0 items-center justify-between border-b px-3 pt-2 pb-2">
            <div className="flex items-center gap-2">
              <Table2 className="h-4 w-4 text-green-500" />
              <span className="text-sm font-medium">{tableLabel || tableName}</span>
              {tableName !== tableLabel && tableName !== (tableLabel || tableName) && (
                <span className="text-muted-foreground text-xs">({tableName})</span>
              )}
            </div>
            <div className="flex items-center gap-2">
              <Button
                variant="outline"
                size="sm"
                onClick={() => setShowTableManagementModal(true)}
                className="h-7 gap-1 text-xs"
              >
                <Settings className="h-3 w-3" />
                테이블 타입 관리
              </Button>
              <Button variant="outline" size="sm" onClick={handleRefresh} className="h-7 w-7 p-0" disabled={loading}>
                <RefreshCw className={cn("h-3 w-3", loading && "animate-spin")} />
              </Button>
              <Button size="sm" onClick={handleSaveAll} className="h-7 gap-1 text-xs" disabled={saving || loading}>
                {saving ? <Loader2 className="h-3 w-3 animate-spin" /> : <Save className="h-3 w-3" />}
                저장
              </Button>
            </div>
          </div>
          <div className="flex min-h-0 flex-1 gap-3 p-3">
            {/* 좌측: 탭 (40%) */}
            <div className="flex min-h-0 w-[40%] flex-col">
              <Tabs value={activeTab} onValueChange={setActiveTab} className="flex min-h-0 flex-1 flex-col">
                <TabsList className="h-8 flex-shrink-0">
                  <TabsTrigger value="columns" className="gap-1 text-xs">
                    <Columns3 className="h-3 w-3" />
                    컬럼 설정
                  </TabsTrigger>
                  <TabsTrigger value="screens" className="gap-1 text-xs">
                    <Monitor className="h-3 w-3" />
                    화면 연동
                  </TabsTrigger>
                  <TabsTrigger value="references" className="gap-1 text-xs">
                    <Eye className="h-3 w-3" />
                    참조 관계
                  </TabsTrigger>
                </TabsList>

                <TabsContent value="columns" className="mt-2 min-h-0 flex-1 overflow-hidden">
                  <ColumnListTab
                    columns={tableColumns.map((col) => ({
                      ...col,
                      isPK: col.columnName === "id" || col.columnName.endsWith("_id"),
                      isFK: (col.inputType as string) === "entity",
                    }))}
                    editedColumns={editedColumns}
                    selectedColumn={selectedColumn}
                    onSelectColumn={setSelectedColumn}
                    loading={loading}
                  />
                </TabsContent>

                <TabsContent value="screens" className="mt-2 min-h-0 flex-1 overflow-hidden">
                  <ScreensTab screensUsingTable={screensUsingTable} loading={loading} />
                </TabsContent>

                <TabsContent value="references" className="mt-2 min-h-0 flex-1 overflow-hidden">
                  <ReferenceTab
                    tableName={tableName}
                    tableLabel={tableLabel}
                    referencedBy={referencedBy}
                    joinColumnRefs={joinColumnRefs}
                    loading={loading}
                  />
                </TabsContent>
              </Tabs>
            </div>

            {/* 우측: 상세 설정 (60%) */}
            <div className="bg-muted/30 flex min-h-0 w-[60%] flex-col rounded-lg border p-3">
              {selectedColumn && mergedColumns.find((c) => c.columnName === selectedColumn) ? (
                <ColumnDetailPanel
                  columnInfo={mergedColumns.find((c) => c.columnName === selectedColumn)!}
                  editedColumn={editedColumns[selectedColumn] || {}}
                  tableOptions={tableOptions}
                  inputTypeOptions={inputTypeOptions}
                  getRefColumnOptions={getRefColumnOptions}
                  loadingRefColumns={loadingRefColumns}
                  onColumnChange={(field, value) => handleColumnChange(selectedColumn, field, value)}
                />
              ) : (
                <div className="text-muted-foreground flex h-full items-center justify-center text-sm">
                  <div className="text-center">
                    <Columns3 className="text-muted-foreground/30 mx-auto h-12 w-12" />
                    <p className="mt-2">왼쪽에서 컬럼을 선택하면</p>
                    <p>상세 설정을 할 수 있습니다.</p>
                  </div>
                </div>
              )}
            </div>
          </div>
        </div>

        {/* 테이블 타입 관리 모달 */}
        <Dialog open={showTableManagementModal} onOpenChange={setShowTableManagementModal}>
          <DialogContent className="flex h-[90vh] max-h-[1000px] w-[95vw] max-w-[1400px] flex-col p-0">
            <div className="flex items-center justify-between border-b p-4">
              <h2 className="text-lg font-semibold">테이블 타입 관리</h2>
              <Button
                variant="ghost"
                size="sm"
                onClick={() => {
                  setShowTableManagementModal(false);
                  loadTableData();
                }}
              >
                <X className="h-4 w-4" />
              </Button>
            </div>
            <div className="flex-1 overflow-hidden">
              <TableManagementPage />
            </div>
          </DialogContent>
        </Dialog>
      </>
    );
  }

  // 기존 모달 모드
  return (
    <>
      <Dialog open={isOpen} onOpenChange={onClose}>
        <DialogContent className="flex h-[85vh] max-h-[900px] w-[95vw] max-w-[1200px] flex-col">
          <DialogHeader className="flex-shrink-0">
            <div className="flex items-center justify-between">
              <DialogTitle className="flex items-center gap-2 text-lg">
                <Table2 className="h-5 w-5 text-green-500" />
                테이블 설정: {tableLabel || tableName}
                {tableName !== tableLabel && tableName !== (tableLabel || tableName) && (
                  <span className="text-muted-foreground text-sm font-normal">({tableName})</span>
                )}
              </DialogTitle>
              <Button
                variant="outline"
                size="sm"
                onClick={() => setShowTableManagementModal(true)}
                className="gap-1 text-xs"
              >
                <Settings className="h-3.5 w-3.5" />
                테이블 타입 관리
              </Button>
            </div>
            <DialogDescription className="text-sm">
              테이블의 컬럼 설정, 조인 관계, 화면 연동 현황을 확인하고 설정합니다.
            </DialogDescription>
          </DialogHeader>

          <div className="flex min-h-0 flex-1 gap-4">
            {/* 좌측: 탭 (40%) */}
            <div className="flex min-h-0 w-[40%] flex-col">
              <Tabs value={activeTab} onValueChange={setActiveTab} className="flex min-h-0 flex-1 flex-col">
                <div className="flex flex-shrink-0 items-center justify-between border-b pb-2">
                  <TabsList className="h-9">
                    <TabsTrigger value="columns" className="gap-1 text-xs">
                      <Columns3 className="h-3.5 w-3.5" />
                      컬럼 설정
                    </TabsTrigger>
                    <TabsTrigger value="screens" className="gap-1 text-xs">
                      <Monitor className="h-3.5 w-3.5" />
                      화면 연동
                    </TabsTrigger>
                    <TabsTrigger value="references" className="gap-1 text-xs">
                      <Eye className="h-3.5 w-3.5" />
                      참조 관계
                    </TabsTrigger>
                  </TabsList>
                  <div className="flex gap-2">
                    <Button
                      variant="outline"
                      size="sm"
                      onClick={handleRefresh}
                      className="h-8 gap-1"
                      disabled={loading}
                    >
                      <RefreshCw className={cn("h-3.5 w-3.5", loading && "animate-spin")} />
                    </Button>
                    <Button size="sm" onClick={handleSaveAll} className="h-8 gap-1" disabled={saving || loading}>
                      {saving ? <Loader2 className="h-3.5 w-3.5 animate-spin" /> : <Save className="h-3.5 w-3.5" />}
                      저장
                    </Button>
                  </div>
                </div>

                {/* 탭 1: 컬럼 설정 */}
                <TabsContent value="columns" className="mt-0 min-h-0 flex-1 overflow-auto">
                  <ColumnListTab
                    columns={mergedColumns}
                    editedColumns={editedColumns}
                    selectedColumn={selectedColumn}
                    onSelectColumn={setSelectedColumn}
                    loading={loading}
                  />
                </TabsContent>

                {/* 탭 2: 화면 연동 */}
                <TabsContent value="screens" className="mt-0 min-h-0 flex-1 overflow-auto p-3">
                  <ScreensTab screensUsingTable={screensUsingTable} loading={loading} />
                </TabsContent>

                {/* 탭 3: 참조 관계 */}
                <TabsContent value="references" className="mt-0 min-h-0 flex-1 overflow-auto p-3">
                  <ReferenceTab
                    tableName={tableName}
                    tableLabel={tableLabel}
                    referencedBy={referencedBy}
                    joinColumnRefs={joinColumnRefs}
                    loading={loading}
                  />
                </TabsContent>
              </Tabs>
            </div>

            {/* 우측: 컬럼 상세 설정 (60%) */}
            <div className="bg-muted/30 flex min-h-0 w-[60%] flex-col rounded-lg border p-4">
              {selectedColumn && selectedColumnInfo ? (
                <ColumnDetailPanel
                  columnInfo={selectedColumnInfo}
                  editedColumn={editedColumns[selectedColumn] || {}}
                  tableOptions={tableOptions}
                  inputTypeOptions={inputTypeOptions}
                  getRefColumnOptions={getRefColumnOptions}
                  loadingRefColumns={loadingRefColumns}
                  onColumnChange={(field, value) => handleColumnChange(selectedColumn, field, value)}
                />
              ) : (
                <div className="text-muted-foreground flex h-full items-center justify-center text-sm">
                  <div className="text-center">
                    <Columns3 className="text-muted-foreground/30 mx-auto h-12 w-12" />
                    <p className="mt-2">왼쪽에서 컬럼을 선택하면</p>
                    <p>상세 설정을 할 수 있습니다.</p>
                  </div>
                </div>
              )}
            </div>
          </div>
        </DialogContent>
      </Dialog>

      {/* 테이블 타입 관리 전체 화면 모달 */}
      <Dialog open={showTableManagementModal} onOpenChange={setShowTableManagementModal}>
        <DialogContent className="h-[95vh] max-w-[98vw] overflow-hidden p-0">
          <div className="flex h-full flex-col">
            {/* 헤더 */}
            <div className="bg-background flex items-center justify-between border-b px-4 py-3">
              <div>
                <DialogTitle className="text-lg font-semibold">테이블 타입 관리</DialogTitle>
                <p className="text-muted-foreground text-xs">데이터베이스 테이블과 컬럼의 타입을 관리합니다</p>
              </div>
              <Button
                variant="ghost"
                size="sm"
                onClick={() => {
                  setShowTableManagementModal(false);
                  // 테이블 데이터 새로고침
                  loadTableData();
                }}
              >
                <X className="h-4 w-4" />
              </Button>
            </div>

            {/* TableManagementPage */}
            <div className="flex-1 overflow-hidden">
              <TableManagementPage />
            </div>
          </div>
        </DialogContent>
      </Dialog>
    </>
  );
}

// ============================================================
// 탭 1: 컬럼 목록
// ============================================================

interface ColumnListTabProps {
  columns: (ColumnTypeInfo & { isPK?: boolean; isFK?: boolean })[];
  editedColumns: Record<string, Partial<ColumnTypeInfo>>;
  selectedColumn: string | null;
  onSelectColumn: (columnName: string) => void;
  loading: boolean;
}

function ColumnListTab({ columns, editedColumns, selectedColumn, onSelectColumn, loading }: ColumnListTabProps) {
  const [searchTerm, setSearchTerm] = useState("");

  const filteredColumns = useMemo(() => {
    if (!searchTerm) return columns;
    const term = searchTerm.toLowerCase();
    return columns.filter(
      (col) => col.columnName.toLowerCase().includes(term) || (col.displayName || "").toLowerCase().includes(term),
    );
  }, [columns, searchTerm]);

  if (loading) {
    return (
      <div className="flex h-64 items-center justify-center">
        <Loader2 className="text-muted-foreground h-8 w-8 animate-spin" />
      </div>
    );
  }

  return (
    <div className="flex h-full flex-col">
      {/* 검색 */}
      <div className="flex-shrink-0 p-3 pb-2">
        <div className="relative">
          <Search className="text-muted-foreground absolute top-1/2 left-3 h-4 w-4 -translate-y-1/2" />
          <Input
            placeholder="컬럼 검색..."
            value={searchTerm}
            onChange={(e) => setSearchTerm(e.target.value)}
            className="h-9 pl-9 text-sm"
          />
        </div>
      </div>

      {/* 통계 */}
      <div className="flex-shrink-0 px-3 pb-2">
        <div className="text-muted-foreground flex gap-2 text-xs">
          <span>전체 {columns.length}개</span>
          <span>•</span>
          <span>PK {columns.filter((c) => c.isPK).length}개</span>
          <span>•</span>
          <span>FK {columns.filter((c) => c.isFK).length}개</span>
        </div>
      </div>

      {/* 컬럼 목록 */}
      <div className="flex-1 overflow-y-auto">
        {filteredColumns.length === 0 ? (
          <div className="text-muted-foreground py-8 text-center text-sm">
            {searchTerm ? "검색 결과가 없습니다." : "컬럼이 없습니다."}
          </div>
        ) : (
          <div className="space-y-1 px-3 pb-3">
            {filteredColumns.map((col) => {
              const edited = editedColumns[col.columnName] || {};
              // editedColumns에서 inputType을 가져옴 (초기화 시 이미 보정됨)
              const inputType = (edited.inputType || col.inputType || "text") as string;
              const isSelected = selectedColumn === col.columnName;

              return (
                <div
                  key={col.columnName}
                  onClick={() => onSelectColumn(col.columnName)}
                  className={cn(
                    "cursor-pointer rounded-lg border p-3 transition-colors",
                    isSelected ? "border-green-300 bg-green-50" : "bg-background hover:bg-muted/50 border-transparent",
                  )}
                >
                  <div className="flex items-center justify-between">
                    <div className="flex items-center gap-2">
                      <span className="text-muted-foreground text-xs">{getInputTypeIcon(inputType)}</span>
                      <span className="text-sm font-medium">
                        {edited.displayName || col.displayName || col.columnName}
                      </span>
                    </div>
                    <div className="flex gap-1">
                      {col.isPK && (
                        <Badge variant="outline" className="bg-orange-100 px-1.5 text-[10px] text-orange-700">
                          <Key className="mr-0.5 h-2.5 w-2.5" />
                          PK
                        </Badge>
                      )}
                      {/* 엔티티 타입이거나 referenceTable이 설정되어 있으면 조인 배지 표시 (FK와 동일 의미) */}
                      {(inputType === "entity" || edited.referenceTable || col.referenceTable) && (
                        <Badge variant="outline" className="bg-blue-100 px-1.5 text-[10px] text-blue-700">
                          <Link2 className="mr-0.5 h-2.5 w-2.5" />
                          조인
                        </Badge>
                      )}
                    </div>
                  </div>
                  <div className="text-muted-foreground mt-1 text-xs">
                    <span className="font-mono">{col.columnName}</span>
                  </div>
                </div>
              );
            })}
          </div>
        )}
      </div>
    </div>
  );
}

// ============================================================
// 컬럼 상세 설정 패널
// ============================================================

interface ColumnDetailPanelProps {
  columnInfo: ColumnTypeInfo & { isPK?: boolean; isFK?: boolean };
  editedColumn: Partial<ColumnTypeInfo>;
  tableOptions: Array<{ value: string; label: string; description?: string }>;
  inputTypeOptions: Array<{ value: string; label: string }>;
  getRefColumnOptions: (refTable: string) => Array<{ value: string; label: string; description?: string }>;
  loadingRefColumns: boolean;
  onColumnChange: (field: string, value: any) => void;
}

function ColumnDetailPanel({
  columnInfo,
  editedColumn,
  tableOptions,
  inputTypeOptions,
  getRefColumnOptions,
  loadingRefColumns,
  onColumnChange,
}: ColumnDetailPanelProps) {
  const currentLabel = editedColumn.displayName ?? columnInfo.displayName ?? "";
  const currentRefTable = editedColumn.referenceTable ?? columnInfo.referenceTable ?? "";
  const currentRefColumn = editedColumn.referenceColumn ?? columnInfo.referenceColumn ?? "";
  const currentDisplayColumn = editedColumn.displayColumn ?? columnInfo.displayColumn ?? "";
  // editedColumn에서 inputType을 가져옴 (초기화 시 이미 보정됨)
  const currentInputType = (editedColumn.inputType ?? columnInfo.inputType ?? "text") as string;

  return (
    <div className="flex h-full flex-col">
      {/* 헤더 */}
      <div className="flex-shrink-0 border-b pb-4">
        <div className="flex items-center gap-2">
          <Settings2 className="h-5 w-5 text-green-500" />
          <h3 className="text-lg font-semibold">컬럼 설정</h3>
        </div>
        <div className="mt-2 flex items-center gap-2">
          <span className="bg-muted rounded px-2 py-0.5 font-mono text-sm">{columnInfo.columnName}</span>
          <span className="text-muted-foreground text-xs">{columnInfo.dataType}</span>
          {columnInfo.isPK && (
            <Badge variant="outline" className="bg-orange-100 text-[10px] text-orange-700">
              Primary Key
            </Badge>
          )}
          {/* 엔티티 타입이거나 referenceTable이 있으면 조인 배지 표시 */}
          {(currentInputType === "entity" || currentRefTable) && (
            <Badge variant="outline" className="bg-blue-100 text-[10px] text-blue-700">
              조인
            </Badge>
          )}
        </div>
      </div>

      {/* 설정 폼 */}
      <div className="flex-1 space-y-6 overflow-y-auto py-4">
        {/* 기본 정보 */}
        <div className="space-y-4">
          <h4 className="text-muted-foreground text-sm font-semibold">기본 정보</h4>

          <div className="space-y-2">
            <Label className="text-xs">표시 라벨</Label>
            <Input
              value={currentLabel}
              onChange={(e) => onColumnChange("displayName", e.target.value)}
              placeholder={columnInfo.columnName}
              className="h-9 text-sm"
            />
            <p className="text-muted-foreground text-[10px]">화면에 표시될 컬럼 이름입니다.</p>
          </div>

          <div className="space-y-2">
            <Label className="text-xs">입력 타입</Label>
            <Select value={currentInputType} onValueChange={(v) => onColumnChange("inputType", v)}>
              <SelectTrigger className="h-9 text-sm">
                <SelectValue placeholder="입력 타입 선택" />
              </SelectTrigger>
              <SelectContent>
                {inputTypeOptions.map((opt) => (
                  <SelectItem key={opt.value} value={opt.value}>
                    <div className="flex items-center gap-2">
                      {getInputTypeIcon(opt.value)}
                      {opt.label}
                    </div>
                  </SelectItem>
                ))}
              </SelectContent>
            </Select>
            <p className="text-muted-foreground text-[10px]">데이터 입력 시 사용할 컴포넌트 유형입니다.</p>
          </div>
        </div>

        {/* 조인 설정 (Entity 타입일 때만) */}
        {currentInputType === "entity" && (
          <div className="space-y-4">
            <h4 className="text-muted-foreground text-sm font-semibold">Entity 조인 설정</h4>

            <div className="space-y-2">
              <Label className="text-xs">참조 테이블</Label>
              <SearchableSelect
                value={currentRefTable || "none"}
                onValueChange={(v) => onColumnChange("referenceTable", v === "none" ? "" : v)}
                options={tableOptions}
                placeholder="테이블 선택"
              />
            </div>

            {currentRefTable && currentRefTable !== "none" && (
              <>
                <div className="space-y-2">
                  <Label className="text-xs">조인 컬럼 (PK)</Label>
                  {loadingRefColumns ? (
                    <div className="text-muted-foreground flex h-9 items-center gap-2 text-sm">
                      <Loader2 className="h-4 w-4 animate-spin" />
                      컬럼 로딩 중...
                    </div>
                  ) : (
                    <SearchableSelect
                      value={currentRefColumn}
                      onValueChange={(v) => onColumnChange("referenceColumn", v)}
                      options={getRefColumnOptions(currentRefTable)}
                      placeholder="컬럼 선택"
                    />
                  )}
                  <p className="text-muted-foreground text-[10px]">이 컬럼과 연결될 참조 테이블의 컬럼입니다.</p>
                </div>

                <div className="space-y-2">
                  <Label className="text-xs">표시 컬럼</Label>
                  {loadingRefColumns ? (
                    <div className="text-muted-foreground flex h-9 items-center gap-2 text-sm">
                      <Loader2 className="h-4 w-4 animate-spin" />
                      컬럼 로딩 중...
                    </div>
                  ) : (
                    <SearchableSelect
                      value={currentDisplayColumn}
                      onValueChange={(v) => onColumnChange("displayColumn", v)}
                      options={getRefColumnOptions(currentRefTable)}
                      placeholder="표시 컬럼 선택"
                    />
                  )}
                  <p className="text-muted-foreground text-[10px]">화면에 실제로 표시될 참조 테이블의 컬럼입니다.</p>
                </div>
              </>
            )}
          </div>
        )}

        {/* 컬럼 정보 (읽기 전용) */}
        <div className="space-y-4">
          <h4 className="text-muted-foreground text-sm font-semibold">컬럼 정보</h4>

          <div className="grid grid-cols-2 gap-4 text-sm">
            <div>
              <span className="text-muted-foreground text-xs">데이터 타입</span>
              <p className="font-mono">{columnInfo.dataType}</p>
            </div>
            <div>
              <span className="text-muted-foreground text-xs">NULL 허용</span>
              <p>{columnInfo.isNullable === "YES" ? "예" : "아니오"}</p>
            </div>
            {columnInfo.maxLength && (
              <div>
                <span className="text-muted-foreground text-xs">최대 길이</span>
                <p>{columnInfo.maxLength}</p>
              </div>
            )}
            {columnInfo.defaultValue && (
              <div>
                <span className="text-muted-foreground text-xs">기본값</span>
                <p className="font-mono text-xs">{columnInfo.defaultValue}</p>
              </div>
            )}
          </div>
        </div>
      </div>
    </div>
  );
}

// ============================================================
// 탭 2: 화면 연동 현황
// ============================================================

interface ScreensTabProps {
  screensUsingTable: ScreenUsingTable[];
  loading: boolean;
}

function ScreensTab({ screensUsingTable, loading }: ScreensTabProps) {
  if (loading) {
    return (
      <div className="flex h-64 items-center justify-center">
        <Loader2 className="text-muted-foreground h-8 w-8 animate-spin" />
      </div>
    );
  }

  return (
    <div className="space-y-4">
      <div className="flex items-center justify-between">
        <h3 className="text-sm font-semibold">이 테이블을 사용하는 화면 ({screensUsingTable.length}개)</h3>
      </div>

      {screensUsingTable.length === 0 ? (
        <div className="text-muted-foreground rounded-lg border border-dashed p-8 text-center text-sm">
          <Monitor className="text-muted-foreground/30 mx-auto h-12 w-12" />
          <p className="mt-2">이 테이블을 사용하는 화면이 없습니다.</p>
        </div>
      ) : (
        <div className="space-y-2">
          {screensUsingTable.map((screen) => (
            <div
              key={screen.screenId}
              className="bg-background hover:bg-muted/50 flex items-center justify-between rounded-lg border p-3"
            >
              <div className="flex items-center gap-3">
                <Monitor className="h-4 w-4 text-blue-500" />
                <div>
                  <p className="text-sm font-medium">{screen.screenName}</p>
                  {screen.screenCode && <p className="text-muted-foreground font-mono text-xs">{screen.screenCode}</p>}
                </div>
              </div>
              <div className="flex items-center gap-2">
                <Badge
                  variant="outline"
                  className={cn(
                    "text-[10px]",
                    screen.tableRole === "main"
                      ? "bg-blue-100 text-blue-700"
                      : screen.tableRole === "filter"
                        ? "bg-purple-100 text-purple-700"
                        : "bg-orange-100 text-orange-700",
                  )}
                >
                  {screen.tableRole === "main"
                    ? "메인 테이블"
                    : screen.tableRole === "filter"
                      ? "필터 테이블"
                      : "조인 테이블"}
                </Badge>
                <Button variant="ghost" size="icon" className="h-7 w-7">
                  <ExternalLink className="h-3.5 w-3.5" />
                </Button>
              </div>
            </div>
          ))}
        </div>
      )}
    </div>
  );
}

// ============================================================
// 탭 3: 참조 관계
// ============================================================

interface ReferenceTabProps {
  tableName: string;
  tableLabel?: string;
  referencedBy: ReferencedBy[];
  joinColumnRefs: JoinColumnRef[];
  loading: boolean;
}

function ReferenceTab({ tableName, tableLabel, referencedBy, joinColumnRefs, loading }: ReferenceTabProps) {
  if (loading) {
    return (
      <div className="flex h-64 items-center justify-center">
        <Loader2 className="text-muted-foreground h-8 w-8 animate-spin" />
      </div>
    );
  }

  return (
    <div className="space-y-6">
      {/* 이 테이블이 참조하는 테이블 */}
      <div className="space-y-2">
        <h3 className="flex items-center gap-2 text-sm font-semibold">
          <ArrowRight className="h-4 w-4 text-orange-500" />이 테이블이 참조하는 테이블 ({joinColumnRefs.length}개)
        </h3>
        {joinColumnRefs.length > 0 ? (
          <div className="space-y-2">
            {joinColumnRefs.map((ref, idx) => (
              <div key={idx} className="flex items-center gap-3 rounded-lg border bg-orange-50/50 p-3">
                <Badge variant="outline" className="bg-orange-100 text-orange-700">
                  {ref.column}
                </Badge>
                <ArrowRight className="text-muted-foreground h-4 w-4" />
                <div className="flex-1">
                  <span className="font-medium">{ref.refTableLabel || ref.refTable}</span>
                  <span className="text-muted-foreground">.{ref.refColumn}</span>
                </div>
              </div>
            ))}
          </div>
        ) : (
          <div className="text-muted-foreground rounded-lg border border-dashed p-4 text-center text-sm">
            참조하는 테이블이 없습니다.
          </div>
        )}
      </div>

      {/* 이 테이블을 참조하는 테이블 */}
      <div className="space-y-2">
        <h3 className="flex items-center gap-2 text-sm font-semibold">
          <Eye className="h-4 w-4 text-green-500" />이 테이블을 참조하는 테이블 ({referencedBy.length}개)
        </h3>
        {referencedBy.length > 0 ? (
          <div className="space-y-2">
            {referencedBy.map((ref, idx) => (
              <div key={idx} className="flex items-center gap-3 rounded-lg border bg-green-50/50 p-3">
                <div className="flex-1">
                  <span className="font-medium">{ref.fromTableLabel || ref.fromTable}</span>
                  <span className="text-muted-foreground">.{ref.fromColumn}</span>
                </div>
                <ArrowRight className="text-muted-foreground h-4 w-4" />
                <Badge variant="outline" className="bg-green-100 text-green-700">
                  {ref.toColumn}
                </Badge>
              </div>
            ))}
          </div>
        ) : (
          <div className="text-muted-foreground rounded-lg border border-dashed p-4 text-center text-sm">
            이 테이블을 참조하는 테이블이 없습니다.
          </div>
        )}
      </div>
    </div>
  );
}

export default TableSettingModal;
