'use client';

import type { ItemPage, SectionTemplate, ItemMasterField, ItemSection } from '@/contexts/ItemMasterContext';
import { FieldDialog } from '../dialogs/FieldDialog';
import { FieldDrawer } from '../dialogs/FieldDrawer';
import { TabManagementDialogs } from '../dialogs/TabManagementDialogs';
import { OptionDialog } from '../dialogs/OptionDialog';
import { ColumnManageDialog } from '../dialogs/ColumnManageDialog';
import { PathEditDialog } from '../dialogs/PathEditDialog';
import { PageDialog } from '../dialogs/PageDialog';
import { SectionDialog } from '../dialogs/SectionDialog';
import { MasterFieldDialog } from '../dialogs/MasterFieldDialog';
import { TemplateFieldDialog } from '../dialogs/TemplateFieldDialog';
import { LoadTemplateDialog } from '../dialogs/LoadTemplateDialog';
import { ColumnDialog } from '../dialogs/ColumnDialog';
import { SectionTemplateDialog } from '../dialogs/SectionTemplateDialog';
import { ImportSectionDialog } from '../dialogs/ImportSectionDialog';
import { ImportFieldDialog } from '../dialogs/ImportFieldDialog';
import type { CustomTab, AttributeSubTab } from '../hooks/useTabManagement';
import type { ConditionalFieldConfig } from '../components/ConditionalDisplayUI';
import type { SectionUsageResponse, FieldUsageResponse } from '@/types/item-master-api';

// 텍스트박스 칼럼 타입 (FieldDialog와 동일)
interface TextboxColumn {
  id: string;
  name: string;
  key: string;
}

// 속성 컬럼 타입 (OptionDialog/ColumnManageDialog 호환)
interface AttributeColumn {
  id: string;
  name: string;
  key: string;
  type: string;
  required: boolean;
}

// 조건부 표시 필드 타입 - 유연한 타입 (FieldDialog.tsx의 FlexibleConditionField와 호환)
interface ConditionField {
  fieldId?: string;
  fieldKey?: string;
  fieldName?: string;
  operator?: string;
  value?: string;
  expectedValue?: string;
  logicOperator?: 'AND' | 'OR';
}

// 조건부 표시 섹션 타입 - 유연한 타입 (FieldDialog.tsx와 호환)
interface ConditionSection {
  sectionId?: string;
  sectionTitle?: string;
  operator?: string;
  value?: string;
  logicOperator?: 'AND' | 'OR';
}

export interface ItemMasterDialogsProps {
  isMobile: boolean;
  selectedPage: ItemPage | null;

  // Tab Management
  isManageTabsDialogOpen: boolean;
  setIsManageTabsDialogOpen: (open: boolean) => void;
  customTabs: CustomTab[];
  moveTabUp: (tabId: string) => void;
  moveTabDown: (tabId: string) => void;
  handleEditTabFromManage: (tabId: string) => void;
  handleDeleteTab: (tabId: string) => void;
  getTabIcon: (iconName: string) => React.ComponentType<{ className?: string }>;
  setIsAddTabDialogOpen: (open: boolean) => void;
  isDeleteTabDialogOpen: boolean;
  setIsDeleteTabDialogOpen: (open: boolean) => void;
  deletingTabId: string | null;
  setDeletingTabId: (id: string | null) => void;
  confirmDeleteTab: () => void;
  isAddTabDialogOpen: boolean;
  editingTabId: string | null;
  setEditingTabId: (id: string | null) => void;
  newTabLabel: string;
  setNewTabLabel: (label: string) => void;
  handleUpdateTab: () => void;
  handleAddTab: () => void;
  isManageAttributeTabsDialogOpen: boolean;
  setIsManageAttributeTabsDialogOpen: (open: boolean) => void;
  attributeSubTabs: AttributeSubTab[];
  moveAttributeTabUp: (tabId: string) => void;
  moveAttributeTabDown: (tabId: string) => void;
  handleDeleteAttributeTab: (tabId: string) => void;
  isDeleteAttributeTabDialogOpen: boolean;
  setIsDeleteAttributeTabDialogOpen: (open: boolean) => void;
  deletingAttributeTabId: string | null;
  setDeletingAttributeTabId: (id: string | null) => void;
  confirmDeleteAttributeTab: () => void;
  isAddAttributeTabDialogOpen: boolean;
  setIsAddAttributeTabDialogOpen: (open: boolean) => void;
  editingAttributeTabId: string | null;
  setEditingAttributeTabId: (id: string | null) => void;
  newAttributeTabLabel: string;
  setNewAttributeTabLabel: (label: string) => void;
  handleUpdateAttributeTab: () => void;
  handleAddAttributeTab: () => void;

  // Option Dialog
  isOptionDialogOpen: boolean;
  setIsOptionDialogOpen: (open: boolean) => void;
  newOptionValue: string;
  setNewOptionValue: (value: string) => void;
  newOptionLabel: string;
  setNewOptionLabel: (value: string) => void;
  newOptionColumnValues: Record<string, string>;
  setNewOptionColumnValues: (values: Record<string, string>) => void;
  newOptionInputType: string;
  setNewOptionInputType: (type: string) => void;
  newOptionRequired: boolean;
  setNewOptionRequired: (required: boolean) => void;
  // string | string[] 모두 지원
  newOptionOptions: string | string[];
  setNewOptionOptions: (options: string | string[]) => void;
  newOptionPlaceholder: string;
  setNewOptionPlaceholder: (placeholder: string) => void;
  newOptionDefaultValue: string;
  setNewOptionDefaultValue: (value: string) => void;
  editingOptionType: string;
  attributeColumns: Record<string, AttributeColumn[]>;
  handleAddOption: () => void;

  // Column Manage Dialog
  isColumnManageDialogOpen: boolean;
  setIsColumnManageDialogOpen: (open: boolean) => void;
  managingColumnType: string;
  setAttributeColumns: React.Dispatch<React.SetStateAction<Record<string, AttributeColumn[]>>>;
  newColumnName: string;
  setNewColumnName: (name: string) => void;
  newColumnKey: string;
  setNewColumnKey: (key: string) => void;
  newColumnType: string;
  setNewColumnType: (type: string) => void;
  newColumnRequired: boolean;
  setNewColumnRequired: (required: boolean) => void;

  // Path Edit Dialog
  editingPathPageId: number | null;
  setEditingPathPageId: (id: number | null) => void;
  editingAbsolutePath: string;
  setEditingAbsolutePath: (path: string) => void;
  updateItemPage: (id: number, updates: Partial<ItemPage>) => void;

  // Page Dialog
  isPageDialogOpen: boolean;
  setIsPageDialogOpen: (open: boolean) => void;
  newPageName: string;
  setNewPageName: (name: string) => void;
  newPageItemType: 'FG' | 'PT' | 'SM' | 'RM' | 'CS';
  setNewPageItemType: React.Dispatch<React.SetStateAction<'FG' | 'PT' | 'SM' | 'RM' | 'CS'>>;
  handleAddPage: () => void;

  // Section Dialog
  isSectionDialogOpen: boolean;
  setIsSectionDialogOpen: (open: boolean) => void;
  newSectionType: 'fields' | 'bom';
  setNewSectionType: (type: 'fields' | 'bom') => void;
  newSectionTitle: string;
  setNewSectionTitle: (title: string) => void;
  newSectionDescription: string;
  setNewSectionDescription: (description: string) => void;
  handleAddSection: () => void;
  // 'new'/'existing' 또는 'custom'/'template' 모두 지원
  sectionInputMode: 'new' | 'existing' | 'custom' | 'template';
  setSectionInputMode: (mode: 'new' | 'existing' | 'custom' | 'template') => void;
  sectionsAsTemplates: SectionTemplate[];
  selectedSectionTemplateId: number | null;
  setSelectedSectionTemplateId: (id: number | null) => void;
  handleLinkTemplate: ((template: SectionTemplate) => void | Promise<void>) | (() => void | Promise<void>);

  // Field Dialog
  isFieldDialogOpen: boolean;
  setIsFieldDialogOpen: (open: boolean) => void;
  selectedSectionForField: number | null;
  editingFieldId: number | null;
  setEditingFieldId: (id: number | null) => void;
  // 'new'/'existing' 또는 'custom'/'master' 모두 지원
  fieldInputMode: 'new' | 'existing' | 'custom' | 'master';
  setFieldInputMode: (mode: 'new' | 'existing' | 'custom' | 'master') => void;
  showMasterFieldList: boolean;
  setShowMasterFieldList: (show: boolean) => void;
  // string 또는 number | null 모두 지원
  selectedMasterFieldId: string | number | null;
  setSelectedMasterFieldId: (id: string | number | null) => void;
  textboxColumns: TextboxColumn[];
  setTextboxColumns: React.Dispatch<React.SetStateAction<TextboxColumn[]>>;
  newFieldConditionEnabled: boolean;
  setNewFieldConditionEnabled: (enabled: boolean) => void;
  newFieldConditionTargetType: 'field' | 'section';
  setNewFieldConditionTargetType: (type: 'field' | 'section') => void;
  // 유연한 조건부 필드 타입
  newFieldConditionFields: ConditionField[] | ConditionalFieldConfig[];
  setNewFieldConditionFields: React.Dispatch<React.SetStateAction<ConditionField[]>> | React.Dispatch<React.SetStateAction<ConditionalFieldConfig[]>>;
  // 유연한 조건부 섹션 타입
  newFieldConditionSections: ConditionSection[] | string[];
  setNewFieldConditionSections: React.Dispatch<React.SetStateAction<ConditionSection[]>> | React.Dispatch<React.SetStateAction<string[]>>;
  tempConditionValue: string;
  setTempConditionValue: (value: string) => void;
  newFieldName: string;
  setNewFieldName: (name: string) => void;
  newFieldKey: string;
  setNewFieldKey: (key: string) => void;
  newFieldInputType: string;
  setNewFieldInputType: (type: string) => void;
  newFieldRequired: boolean;
  setNewFieldRequired: (required: boolean) => void;
  newFieldDescription: string;
  setNewFieldDescription: (description: string) => void;
  // string | string[] 모두 지원
  newFieldOptions: string | string[];
  setNewFieldOptions: ((options: string | string[]) => void) | React.Dispatch<React.SetStateAction<string[]>>;
  itemMasterFields: ItemMasterField[];
  handleAddField: () => void;
  isColumnDialogOpen: boolean;
  setIsColumnDialogOpen: (open: boolean) => void;
  editingColumnId: string | null;
  setEditingColumnId: (id: string | null) => void;
  columnName: string;
  setColumnName: (name: string) => void;
  columnKey: string;
  setColumnKey: (key: string) => void;

  // Master Field Dialog
  isMasterFieldDialogOpen: boolean;
  setIsMasterFieldDialogOpen: (open: boolean) => void;
  editingMasterFieldId: number | null;
  setEditingMasterFieldId: (id: number | null) => void;
  newMasterFieldName: string;
  setNewMasterFieldName: (name: string) => void;
  newMasterFieldKey: string;
  setNewMasterFieldKey: (key: string) => void;
  newMasterFieldInputType: string;
  setNewMasterFieldInputType: (type: string) => void;
  newMasterFieldRequired: boolean;
  setNewMasterFieldRequired: (required: boolean) => void;
  newMasterFieldCategory: string;
  setNewMasterFieldCategory: (category: string) => void;
  newMasterFieldDescription: string;
  setNewMasterFieldDescription: (description: string) => void;
  newMasterFieldOptions: string[];
  setNewMasterFieldOptions: React.Dispatch<React.SetStateAction<string[]>>;
  newMasterFieldAttributeType: string;
  setNewMasterFieldAttributeType: (type: string) => void;
  newMasterFieldMultiColumn: boolean;
  setNewMasterFieldMultiColumn: (multiColumn: boolean) => void;
  newMasterFieldColumnCount: number;
  setNewMasterFieldColumnCount: (count: number) => void;
  newMasterFieldColumnNames: string[];
  setNewMasterFieldColumnNames: React.Dispatch<React.SetStateAction<string[]>>;
  handleUpdateMasterField: () => void;
  handleAddMasterField: () => void;

  // Section Template Dialog
  isSectionTemplateDialogOpen: boolean;
  setIsSectionTemplateDialogOpen: (open: boolean) => void;
  editingSectionTemplateId: number | null;
  setEditingSectionTemplateId: (id: number | null) => void;
  newSectionTemplateTitle: string;
  setNewSectionTemplateTitle: (title: string) => void;
  newSectionTemplateDescription: string;
  setNewSectionTemplateDescription: (description: string) => void;
  // string 또는 string[] 모두 지원
  newSectionTemplateCategory: string | string[];
  setNewSectionTemplateCategory: (category: string | string[]) => void;
  newSectionTemplateType: 'fields' | 'bom';
  setNewSectionTemplateType: (type: 'fields' | 'bom') => void;
  handleUpdateSectionTemplate: () => void;
  handleAddSectionTemplate: () => void;

  // Template Field Dialog
  isTemplateFieldDialogOpen: boolean;
  setIsTemplateFieldDialogOpen: (open: boolean) => void;
  editingTemplateFieldId: string | null;
  setEditingTemplateFieldId: (id: string | null) => void;
  templateFieldName: string;
  setTemplateFieldName: (name: string) => void;
  templateFieldKey: string;
  setTemplateFieldKey: (key: string) => void;
  templateFieldInputType: string;
  setTemplateFieldInputType: (type: string) => void;
  templateFieldRequired: boolean;
  setTemplateFieldRequired: (required: boolean) => void;
  templateFieldOptions: string[];
  setTemplateFieldOptions: React.Dispatch<React.SetStateAction<string[]>>;
  templateFieldDescription: string;
  setTemplateFieldDescription: (description: string) => void;
  templateFieldMultiColumn: boolean;
  setTemplateFieldMultiColumn: (multiColumn: boolean) => void;
  templateFieldColumnCount: number;
  setTemplateFieldColumnCount: (count: number) => void;
  templateFieldColumnNames: string[];
  setTemplateFieldColumnNames: React.Dispatch<React.SetStateAction<string[]>>;
  // () => void 또는 () => Promise<void> 모두 지원
  handleAddTemplateField: (() => void) | (() => Promise<void>);
  // 'new'/'existing' 또는 'custom'/'master' 모두 지원
  templateFieldInputMode: 'new' | 'existing' | 'custom' | 'master';
  setTemplateFieldInputMode: (mode: 'new' | 'existing' | 'custom' | 'master') => void;
  templateFieldShowMasterFieldList: boolean;
  setTemplateFieldShowMasterFieldList: (show: boolean) => void;
  // string 또는 number | null 모두 지원
  templateFieldSelectedMasterFieldId: string | number | null;
  setTemplateFieldSelectedMasterFieldId: (id: string | number | null) => void;

  // Load Template Dialog
  isLoadTemplateDialogOpen: boolean;
  setIsLoadTemplateDialogOpen: (open: boolean) => void;
  sectionTemplates: SectionTemplate[];
  // string | number | null 모두 지원
  selectedTemplateId: string | number | null;
  setSelectedTemplateId: (id: string | number | null) => void;
  handleLoadTemplate: () => void;

  // Import Section Dialog
  isImportSectionDialogOpen: boolean;
  setIsImportSectionDialogOpen: (open: boolean) => void;
  independentSections: ItemSection[];
  selectedImportSectionId: number | null;
  setSelectedImportSectionId: (id: number | null) => void;
  // () => void 또는 () => Promise<void> 모두 지원
  handleImportSection: (() => void) | (() => Promise<void>);
  refreshIndependentSections: () => void;
  // 유연한 반환 타입 - SectionUsageResponse 또는 간단한 객체
  getSectionUsage: (sectionId: number) => Promise<SectionUsageResponse | { pages: { id: number; name: string }[] }>;

  // Import Field Dialog
  isImportFieldDialogOpen: boolean;
  setIsImportFieldDialogOpen: (open: boolean) => void;
  selectedImportFieldId: number | null;
  setSelectedImportFieldId: (id: number | null) => void;
  // () => void 또는 () => Promise<void> 모두 지원
  handleImportField: (() => void) | (() => Promise<void>);
  refreshIndependentFields: () => void;
  // 유연한 반환 타입 - FieldUsageResponse 또는 간단한 객체
  getFieldUsage: (fieldId: number) => Promise<FieldUsageResponse | { sections: { id: number; title: string }[] }>;
  importFieldTargetSectionId: number | null;
}

export function ItemMasterDialogs({
  isMobile,
  selectedPage,

  // Tab Management
  isManageTabsDialogOpen,
  setIsManageTabsDialogOpen,
  customTabs,
  moveTabUp,
  moveTabDown,
  handleEditTabFromManage,
  handleDeleteTab,
  getTabIcon,
  setIsAddTabDialogOpen,
  isDeleteTabDialogOpen,
  setIsDeleteTabDialogOpen,
  deletingTabId,
  setDeletingTabId,
  confirmDeleteTab,
  isAddTabDialogOpen,
  editingTabId,
  setEditingTabId,
  newTabLabel,
  setNewTabLabel,
  handleUpdateTab,
  handleAddTab,
  isManageAttributeTabsDialogOpen,
  setIsManageAttributeTabsDialogOpen,
  attributeSubTabs,
  moveAttributeTabUp,
  moveAttributeTabDown,
  handleDeleteAttributeTab,
  isDeleteAttributeTabDialogOpen,
  setIsDeleteAttributeTabDialogOpen,
  deletingAttributeTabId,
  setDeletingAttributeTabId,
  confirmDeleteAttributeTab,
  isAddAttributeTabDialogOpen,
  setIsAddAttributeTabDialogOpen,
  editingAttributeTabId,
  setEditingAttributeTabId,
  newAttributeTabLabel,
  setNewAttributeTabLabel,
  handleUpdateAttributeTab,
  handleAddAttributeTab,

  // Option Dialog
  isOptionDialogOpen,
  setIsOptionDialogOpen,
  newOptionValue,
  setNewOptionValue,
  newOptionLabel,
  setNewOptionLabel,
  newOptionColumnValues,
  setNewOptionColumnValues,
  newOptionInputType,
  setNewOptionInputType,
  newOptionRequired,
  setNewOptionRequired,
  newOptionOptions,
  setNewOptionOptions,
  newOptionPlaceholder,
  setNewOptionPlaceholder,
  newOptionDefaultValue,
  setNewOptionDefaultValue,
  editingOptionType,
  attributeColumns,
  handleAddOption,

  // Column Manage Dialog
  isColumnManageDialogOpen,
  setIsColumnManageDialogOpen,
  managingColumnType,
  setAttributeColumns,
  newColumnName,
  setNewColumnName,
  newColumnKey,
  setNewColumnKey,
  newColumnType,
  setNewColumnType,
  newColumnRequired,
  setNewColumnRequired,

  // Path Edit Dialog
  editingPathPageId,
  setEditingPathPageId,
  editingAbsolutePath,
  setEditingAbsolutePath,
  updateItemPage,

  // Page Dialog
  isPageDialogOpen,
  setIsPageDialogOpen,
  newPageName,
  setNewPageName,
  newPageItemType,
  setNewPageItemType,
  handleAddPage,

  // Section Dialog
  isSectionDialogOpen,
  setIsSectionDialogOpen,
  newSectionType,
  setNewSectionType,
  newSectionTitle,
  setNewSectionTitle,
  newSectionDescription,
  setNewSectionDescription,
  handleAddSection,
  sectionInputMode,
  setSectionInputMode,
  sectionsAsTemplates,
  selectedSectionTemplateId,
  setSelectedSectionTemplateId,
  handleLinkTemplate,

  // Field Dialog
  isFieldDialogOpen,
  setIsFieldDialogOpen,
  selectedSectionForField,
  editingFieldId,
  setEditingFieldId,
  fieldInputMode,
  setFieldInputMode,
  showMasterFieldList,
  setShowMasterFieldList,
  selectedMasterFieldId,
  setSelectedMasterFieldId,
  textboxColumns,
  setTextboxColumns,
  newFieldConditionEnabled,
  setNewFieldConditionEnabled,
  newFieldConditionTargetType,
  setNewFieldConditionTargetType,
  newFieldConditionFields,
  setNewFieldConditionFields,
  newFieldConditionSections,
  setNewFieldConditionSections,
  tempConditionValue,
  setTempConditionValue,
  newFieldName,
  setNewFieldName,
  newFieldKey,
  setNewFieldKey,
  newFieldInputType,
  setNewFieldInputType,
  newFieldRequired,
  setNewFieldRequired,
  newFieldDescription,
  setNewFieldDescription,
  newFieldOptions,
  setNewFieldOptions,
  itemMasterFields,
  handleAddField,
  isColumnDialogOpen,
  setIsColumnDialogOpen,
  editingColumnId,
  setEditingColumnId,
  columnName,
  setColumnName,
  columnKey,
  setColumnKey,

  // Master Field Dialog
  isMasterFieldDialogOpen,
  setIsMasterFieldDialogOpen,
  editingMasterFieldId,
  setEditingMasterFieldId,
  newMasterFieldName,
  setNewMasterFieldName,
  newMasterFieldKey,
  setNewMasterFieldKey,
  newMasterFieldInputType,
  setNewMasterFieldInputType,
  newMasterFieldRequired,
  setNewMasterFieldRequired,
  newMasterFieldCategory,
  setNewMasterFieldCategory,
  newMasterFieldDescription,
  setNewMasterFieldDescription,
  newMasterFieldOptions,
  setNewMasterFieldOptions,
  newMasterFieldAttributeType,
  setNewMasterFieldAttributeType,
  newMasterFieldMultiColumn,
  setNewMasterFieldMultiColumn,
  newMasterFieldColumnCount,
  setNewMasterFieldColumnCount,
  newMasterFieldColumnNames,
  setNewMasterFieldColumnNames,
  handleUpdateMasterField,
  handleAddMasterField,

  // Section Template Dialog
  isSectionTemplateDialogOpen,
  setIsSectionTemplateDialogOpen,
  editingSectionTemplateId,
  setEditingSectionTemplateId,
  newSectionTemplateTitle,
  setNewSectionTemplateTitle,
  newSectionTemplateDescription,
  setNewSectionTemplateDescription,
  newSectionTemplateCategory,
  setNewSectionTemplateCategory,
  newSectionTemplateType,
  setNewSectionTemplateType,
  handleUpdateSectionTemplate,
  handleAddSectionTemplate,

  // Template Field Dialog
  isTemplateFieldDialogOpen,
  setIsTemplateFieldDialogOpen,
  editingTemplateFieldId,
  setEditingTemplateFieldId,
  templateFieldName,
  setTemplateFieldName,
  templateFieldKey,
  setTemplateFieldKey,
  templateFieldInputType,
  setTemplateFieldInputType,
  templateFieldRequired,
  setTemplateFieldRequired,
  templateFieldOptions,
  setTemplateFieldOptions,
  templateFieldDescription,
  setTemplateFieldDescription,
  templateFieldMultiColumn,
  setTemplateFieldMultiColumn,
  templateFieldColumnCount,
  setTemplateFieldColumnCount,
  templateFieldColumnNames,
  setTemplateFieldColumnNames,
  handleAddTemplateField,
  templateFieldInputMode,
  setTemplateFieldInputMode,
  templateFieldShowMasterFieldList,
  setTemplateFieldShowMasterFieldList,
  templateFieldSelectedMasterFieldId,
  setTemplateFieldSelectedMasterFieldId,

  // Load Template Dialog
  isLoadTemplateDialogOpen,
  setIsLoadTemplateDialogOpen,
  sectionTemplates,
  selectedTemplateId,
  setSelectedTemplateId,
  handleLoadTemplate,

  // Import Section Dialog
  isImportSectionDialogOpen,
  setIsImportSectionDialogOpen,
  independentSections,
  selectedImportSectionId,
  setSelectedImportSectionId,
  handleImportSection,
  refreshIndependentSections,
  getSectionUsage,

  // Import Field Dialog
  isImportFieldDialogOpen,
  setIsImportFieldDialogOpen,
  selectedImportFieldId,
  setSelectedImportFieldId,
  handleImportField,
  refreshIndependentFields,
  getFieldUsage,
  importFieldTargetSectionId,
}: ItemMasterDialogsProps) {
  return (
    <>
      <TabManagementDialogs
        isManageTabsDialogOpen={isManageTabsDialogOpen}
        setIsManageTabsDialogOpen={setIsManageTabsDialogOpen}
        customTabs={customTabs}
        moveTabUp={moveTabUp}
        moveTabDown={moveTabDown}
        handleEditTabFromManage={handleEditTabFromManage}
        handleDeleteTab={handleDeleteTab}
        getTabIcon={getTabIcon}
        setIsAddTabDialogOpen={setIsAddTabDialogOpen}
        isDeleteTabDialogOpen={isDeleteTabDialogOpen}
        setIsDeleteTabDialogOpen={setIsDeleteTabDialogOpen}
        deletingTabId={deletingTabId}
        setDeletingTabId={setDeletingTabId}
        confirmDeleteTab={confirmDeleteTab}
        isAddTabDialogOpen={isAddTabDialogOpen}
        editingTabId={editingTabId}
        setEditingTabId={setEditingTabId}
        newTabLabel={newTabLabel}
        setNewTabLabel={setNewTabLabel}
        handleUpdateTab={handleUpdateTab}
        handleAddTab={handleAddTab}
        isManageAttributeTabsDialogOpen={isManageAttributeTabsDialogOpen}
        setIsManageAttributeTabsDialogOpen={setIsManageAttributeTabsDialogOpen}
        attributeSubTabs={attributeSubTabs}
        moveAttributeTabUp={moveAttributeTabUp}
        moveAttributeTabDown={moveAttributeTabDown}
        handleDeleteAttributeTab={handleDeleteAttributeTab}
        isDeleteAttributeTabDialogOpen={isDeleteAttributeTabDialogOpen}
        setIsDeleteAttributeTabDialogOpen={setIsDeleteAttributeTabDialogOpen}
        deletingAttributeTabId={deletingAttributeTabId}
        setDeletingAttributeTabId={setDeletingAttributeTabId}
        confirmDeleteAttributeTab={confirmDeleteAttributeTab}
        isAddAttributeTabDialogOpen={isAddAttributeTabDialogOpen}
        setIsAddAttributeTabDialogOpen={setIsAddAttributeTabDialogOpen}
        editingAttributeTabId={editingAttributeTabId}
        setEditingAttributeTabId={setEditingAttributeTabId}
        newAttributeTabLabel={newAttributeTabLabel}
        setNewAttributeTabLabel={setNewAttributeTabLabel}
        handleUpdateAttributeTab={handleUpdateAttributeTab}
        handleAddAttributeTab={handleAddAttributeTab}
      />

      <OptionDialog
        isOpen={isOptionDialogOpen}
        setIsOpen={setIsOptionDialogOpen}
        newOptionValue={newOptionValue}
        setNewOptionValue={setNewOptionValue}
        newOptionLabel={newOptionLabel}
        setNewOptionLabel={setNewOptionLabel}
        newOptionColumnValues={newOptionColumnValues}
        setNewOptionColumnValues={setNewOptionColumnValues}
        newOptionInputType={newOptionInputType}
        setNewOptionInputType={setNewOptionInputType}
        newOptionRequired={newOptionRequired}
        setNewOptionRequired={setNewOptionRequired}
        newOptionOptions={newOptionOptions}
        setNewOptionOptions={setNewOptionOptions}
        newOptionPlaceholder={newOptionPlaceholder}
        setNewOptionPlaceholder={setNewOptionPlaceholder}
        newOptionDefaultValue={newOptionDefaultValue}
        setNewOptionDefaultValue={setNewOptionDefaultValue}
        editingOptionType={editingOptionType}
        attributeSubTabs={attributeSubTabs}
        attributeColumns={attributeColumns}
        handleAddOption={handleAddOption}
      />

      <ColumnManageDialog
        isOpen={isColumnManageDialogOpen}
        setIsOpen={setIsColumnManageDialogOpen}
        managingColumnType={managingColumnType}
        attributeSubTabs={attributeSubTabs}
        attributeColumns={attributeColumns}
        setAttributeColumns={setAttributeColumns}
        newColumnName={newColumnName}
        setNewColumnName={setNewColumnName}
        newColumnKey={newColumnKey}
        setNewColumnKey={setNewColumnKey}
        newColumnType={newColumnType}
        setNewColumnType={setNewColumnType}
        newColumnRequired={newColumnRequired}
        setNewColumnRequired={setNewColumnRequired}
      />

      <PathEditDialog
        editingPathPageId={editingPathPageId}
        setEditingPathPageId={setEditingPathPageId}
        editingAbsolutePath={editingAbsolutePath}
        setEditingAbsolutePath={setEditingAbsolutePath}
        updateItemPage={updateItemPage}
        trackChange={() => {}}
      />

      <PageDialog
        isPageDialogOpen={isPageDialogOpen}
        setIsPageDialogOpen={setIsPageDialogOpen}
        newPageName={newPageName}
        setNewPageName={setNewPageName}
        newPageItemType={newPageItemType}
        setNewPageItemType={setNewPageItemType}
        handleAddPage={handleAddPage}
      />

      <SectionDialog
        isSectionDialogOpen={isSectionDialogOpen}
        setIsSectionDialogOpen={setIsSectionDialogOpen}
        newSectionType={newSectionType}
        setNewSectionType={setNewSectionType}
        newSectionTitle={newSectionTitle}
        setNewSectionTitle={setNewSectionTitle}
        newSectionDescription={newSectionDescription}
        setNewSectionDescription={setNewSectionDescription}
        handleAddSection={handleAddSection}
        sectionInputMode={sectionInputMode}
        setSectionInputMode={setSectionInputMode}
        sectionTemplates={sectionsAsTemplates}
        selectedTemplateId={selectedSectionTemplateId}
        setSelectedTemplateId={setSelectedSectionTemplateId}
        handleLinkTemplate={handleLinkTemplate}
      />

      {/* 항목 추가/수정 다이얼로그 - 데스크톱 */}
      {!isMobile && (
        <FieldDialog
          isOpen={isFieldDialogOpen}
          onOpenChange={setIsFieldDialogOpen}
          editingFieldId={editingFieldId}
          setEditingFieldId={setEditingFieldId}
          fieldInputMode={fieldInputMode}
          setFieldInputMode={setFieldInputMode}
          showMasterFieldList={showMasterFieldList}
          setShowMasterFieldList={setShowMasterFieldList}
          selectedMasterFieldId={selectedMasterFieldId}
          setSelectedMasterFieldId={setSelectedMasterFieldId}
          textboxColumns={textboxColumns}
          setTextboxColumns={setTextboxColumns}
          newFieldConditionEnabled={newFieldConditionEnabled}
          setNewFieldConditionEnabled={setNewFieldConditionEnabled}
          newFieldConditionTargetType={newFieldConditionTargetType}
          setNewFieldConditionTargetType={setNewFieldConditionTargetType}
          newFieldConditionFields={newFieldConditionFields}
          setNewFieldConditionFields={setNewFieldConditionFields}
          newFieldConditionSections={newFieldConditionSections}
          setNewFieldConditionSections={setNewFieldConditionSections}
          tempConditionValue={tempConditionValue}
          setTempConditionValue={setTempConditionValue}
          newFieldName={newFieldName}
          setNewFieldName={setNewFieldName}
          newFieldKey={newFieldKey}
          setNewFieldKey={setNewFieldKey}
          newFieldInputType={newFieldInputType}
          setNewFieldInputType={setNewFieldInputType}
          newFieldRequired={newFieldRequired}
          setNewFieldRequired={setNewFieldRequired}
          newFieldDescription={newFieldDescription}
          setNewFieldDescription={setNewFieldDescription}
          newFieldOptions={newFieldOptions}
          setNewFieldOptions={setNewFieldOptions}
          selectedSectionForField={selectedPage?.sections.find(s => s.id === selectedSectionForField) || null}
          selectedPage={selectedPage || null}
          itemMasterFields={itemMasterFields}
          handleAddField={handleAddField}
          setIsColumnDialogOpen={setIsColumnDialogOpen}
          setEditingColumnId={setEditingColumnId}
          setColumnName={setColumnName}
          setColumnKey={setColumnKey}
        />
      )}

      {/* 항목 추가/수정 다이얼로그 - 모바일 (바텀시트) */}
      {isMobile && (
        <FieldDrawer
          isOpen={isFieldDialogOpen}
          onOpenChange={setIsFieldDialogOpen}
          editingFieldId={editingFieldId}
          setEditingFieldId={setEditingFieldId}
          fieldInputMode={fieldInputMode}
          setFieldInputMode={setFieldInputMode}
          showMasterFieldList={showMasterFieldList}
          setShowMasterFieldList={setShowMasterFieldList}
          selectedMasterFieldId={selectedMasterFieldId}
          setSelectedMasterFieldId={setSelectedMasterFieldId}
          textboxColumns={textboxColumns}
          setTextboxColumns={setTextboxColumns}
          newFieldConditionEnabled={newFieldConditionEnabled}
          setNewFieldConditionEnabled={setNewFieldConditionEnabled}
          newFieldConditionTargetType={newFieldConditionTargetType}
          setNewFieldConditionTargetType={setNewFieldConditionTargetType}
          newFieldConditionFields={newFieldConditionFields}
          setNewFieldConditionFields={setNewFieldConditionFields}
          newFieldConditionSections={newFieldConditionSections}
          setNewFieldConditionSections={setNewFieldConditionSections}
          tempConditionValue={tempConditionValue}
          setTempConditionValue={setTempConditionValue}
          newFieldName={newFieldName}
          setNewFieldName={setNewFieldName}
          newFieldKey={newFieldKey}
          setNewFieldKey={setNewFieldKey}
          newFieldInputType={newFieldInputType}
          setNewFieldInputType={setNewFieldInputType}
          newFieldRequired={newFieldRequired}
          setNewFieldRequired={setNewFieldRequired}
          newFieldDescription={newFieldDescription}
          setNewFieldDescription={setNewFieldDescription}
          newFieldOptions={newFieldOptions}
          setNewFieldOptions={setNewFieldOptions}
          selectedSectionForField={selectedPage?.sections.find(s => s.id === selectedSectionForField) || null}
          selectedPage={selectedPage || null}
          itemMasterFields={itemMasterFields}
          handleAddField={handleAddField}
          setIsColumnDialogOpen={setIsColumnDialogOpen}
          setEditingColumnId={setEditingColumnId}
          setColumnName={setColumnName}
          setColumnKey={setColumnKey}
        />
      )}

      {/* 텍스트박스 컬럼 추가/수정 다이얼로그 */}
      <ColumnDialog
        isColumnDialogOpen={isColumnDialogOpen}
        setIsColumnDialogOpen={setIsColumnDialogOpen}
        editingColumnId={editingColumnId}
        setEditingColumnId={setEditingColumnId}
        columnName={columnName}
        setColumnName={setColumnName}
        columnKey={columnKey}
        setColumnKey={setColumnKey}
        textboxColumns={textboxColumns}
        setTextboxColumns={setTextboxColumns}
      />

      <MasterFieldDialog
        isMasterFieldDialogOpen={isMasterFieldDialogOpen}
        setIsMasterFieldDialogOpen={setIsMasterFieldDialogOpen}
        editingMasterFieldId={editingMasterFieldId}
        setEditingMasterFieldId={setEditingMasterFieldId}
        newMasterFieldName={newMasterFieldName}
        setNewMasterFieldName={setNewMasterFieldName}
        newMasterFieldKey={newMasterFieldKey}
        setNewMasterFieldKey={setNewMasterFieldKey}
        newMasterFieldInputType={newMasterFieldInputType}
        setNewMasterFieldInputType={setNewMasterFieldInputType}
        newMasterFieldRequired={newMasterFieldRequired}
        setNewMasterFieldRequired={setNewMasterFieldRequired}
        newMasterFieldCategory={newMasterFieldCategory}
        setNewMasterFieldCategory={setNewMasterFieldCategory}
        newMasterFieldDescription={newMasterFieldDescription}
        setNewMasterFieldDescription={setNewMasterFieldDescription}
        newMasterFieldOptions={newMasterFieldOptions}
        setNewMasterFieldOptions={setNewMasterFieldOptions}
        newMasterFieldAttributeType={newMasterFieldAttributeType}
        setNewMasterFieldAttributeType={setNewMasterFieldAttributeType}
        newMasterFieldMultiColumn={newMasterFieldMultiColumn}
        setNewMasterFieldMultiColumn={setNewMasterFieldMultiColumn}
        newMasterFieldColumnCount={newMasterFieldColumnCount}
        setNewMasterFieldColumnCount={setNewMasterFieldColumnCount}
        newMasterFieldColumnNames={newMasterFieldColumnNames}
        setNewMasterFieldColumnNames={setNewMasterFieldColumnNames}
        handleUpdateMasterField={handleUpdateMasterField}
        handleAddMasterField={handleAddMasterField}
      />

      <SectionTemplateDialog
        isSectionTemplateDialogOpen={isSectionTemplateDialogOpen}
        setIsSectionTemplateDialogOpen={setIsSectionTemplateDialogOpen}
        editingSectionTemplateId={editingSectionTemplateId}
        setEditingSectionTemplateId={setEditingSectionTemplateId}
        newSectionTemplateTitle={newSectionTemplateTitle}
        setNewSectionTemplateTitle={setNewSectionTemplateTitle}
        newSectionTemplateDescription={newSectionTemplateDescription}
        setNewSectionTemplateDescription={setNewSectionTemplateDescription}
        newSectionTemplateCategory={newSectionTemplateCategory}
        setNewSectionTemplateCategory={setNewSectionTemplateCategory}
        newSectionTemplateType={newSectionTemplateType}
        setNewSectionTemplateType={setNewSectionTemplateType}
        handleUpdateSectionTemplate={handleUpdateSectionTemplate}
        handleAddSectionTemplate={handleAddSectionTemplate}
      />

      <TemplateFieldDialog
        isTemplateFieldDialogOpen={isTemplateFieldDialogOpen}
        setIsTemplateFieldDialogOpen={setIsTemplateFieldDialogOpen}
        editingTemplateFieldId={editingTemplateFieldId}
        setEditingTemplateFieldId={setEditingTemplateFieldId}
        templateFieldName={templateFieldName}
        setTemplateFieldName={setTemplateFieldName}
        templateFieldKey={templateFieldKey}
        setTemplateFieldKey={setTemplateFieldKey}
        templateFieldInputType={templateFieldInputType}
        setTemplateFieldInputType={setTemplateFieldInputType}
        templateFieldRequired={templateFieldRequired}
        setTemplateFieldRequired={setTemplateFieldRequired}
        templateFieldOptions={templateFieldOptions}
        setTemplateFieldOptions={setTemplateFieldOptions}
        templateFieldDescription={templateFieldDescription}
        setTemplateFieldDescription={setTemplateFieldDescription}
        templateFieldMultiColumn={templateFieldMultiColumn}
        setTemplateFieldMultiColumn={setTemplateFieldMultiColumn}
        templateFieldColumnCount={templateFieldColumnCount}
        setTemplateFieldColumnCount={setTemplateFieldColumnCount}
        templateFieldColumnNames={templateFieldColumnNames}
        setTemplateFieldColumnNames={setTemplateFieldColumnNames}
        handleAddTemplateField={handleAddTemplateField}
        itemMasterFields={itemMasterFields}
        templateFieldInputMode={templateFieldInputMode}
        setTemplateFieldInputMode={setTemplateFieldInputMode}
        showMasterFieldList={templateFieldShowMasterFieldList}
        setShowMasterFieldList={setTemplateFieldShowMasterFieldList}
        selectedMasterFieldId={templateFieldSelectedMasterFieldId}
        setSelectedMasterFieldId={setTemplateFieldSelectedMasterFieldId}
      />

      <LoadTemplateDialog
        isLoadTemplateDialogOpen={isLoadTemplateDialogOpen}
        setIsLoadTemplateDialogOpen={setIsLoadTemplateDialogOpen}
        sectionTemplates={sectionTemplates}
        selectedTemplateId={selectedTemplateId}
        setSelectedTemplateId={setSelectedTemplateId}
        handleLoadTemplate={handleLoadTemplate}
      />

      {/* 섹션 불러오기 다이얼로그 */}
      <ImportSectionDialog
        isOpen={isImportSectionDialogOpen}
        setIsOpen={setIsImportSectionDialogOpen}
        independentSections={independentSections}
        selectedSectionId={selectedImportSectionId}
        setSelectedSectionId={setSelectedImportSectionId}
        onImport={handleImportSection}
        onRefresh={refreshIndependentSections as () => Promise<void>}
        onGetUsage={getSectionUsage as (sectionId: number) => Promise<SectionUsageResponse>}
      />

      {/* 필드 불러오기 다이얼로그 */}
      <ImportFieldDialog
        isOpen={isImportFieldDialogOpen}
        setIsOpen={setIsImportFieldDialogOpen}
        fields={itemMasterFields}
        selectedFieldId={selectedImportFieldId}
        setSelectedFieldId={setSelectedImportFieldId}
        onImport={handleImportField}
        onRefresh={refreshIndependentFields as () => Promise<void>}
        onGetUsage={getFieldUsage as (fieldId: number) => Promise<FieldUsageResponse>}
        targetSectionTitle={
          importFieldTargetSectionId
            ? selectedPage?.sections.find(s => s.id === importFieldTargetSectionId)?.title
            : undefined
        }
      />
    </>
  );
}
