// 테이블 타입관리 관련 타입 정의

export interface TableInfo {
  tableName: string;
  displayName: string;
  description: string;
  columnCount: number;
}

export interface ColumnTypeInfo {
  tableName?: string;
  columnName: string;
  displayName: string;
  dataType: string; // DB 데이터 타입 (varchar, integer 등)
  dbType?: string; // DB 타입 (추가됨)
  webType?: string; // 웹 타입 (추가됨)
  inputType: string; // 입력 타입 (text, number, date, code, entity, select, checkbox, radio)
  detailSettings: string;
  description: string;
  isNullable: string;
  isPrimaryKey: boolean;
  defaultValue?: string;
  maxLength?: number;
  numericPrecision?: number;
  numericScale?: number;
  codeCategory?: string;
  codeValue?: string;
  referenceTable?: string;
  referenceColumn?: string;
  displayColumn?: string; // 🎯 Entity 조인에서 표시할 컬럼명
  displayOrder?: number;
  isVisible?: boolean;
}

export interface ColumnSettings {
  columnName?: string; // 컬럼명 (업데이트 시 필요)
  columnLabel: string; // 컬럼 표시명
  inputType: string; // 입력 타입 (text, number, date, code, entity, select, checkbox, radio)
  detailSettings: string; // 상세 설정
  codeCategory: string; // 코드 카테고리
  codeValue: string; // 코드 값
  referenceTable: string; // 참조 테이블
  referenceColumn: string; // 참조 컬럼
  displayColumn?: string; // 🎯 Entity 조인에서 표시할 컬럼명
  displayOrder?: number; // 표시 순서
  isVisible?: boolean; // 표시 여부
  categoryRef?: string | null; // 카테고리 참조
}

export interface TableLabels {
  tableName: string;
  tableLabel?: string;
  description?: string;
  createdDate?: Date;
  updatedDate?: Date;
}

export interface ColumnLabels {
  id?: number;
  tableName: string;
  columnName: string;
  columnLabel?: string;
  webType?: string;
  detailSettings?: string;
  description?: string;
  displayOrder?: number;
  isVisible?: boolean;
  codeCategory?: string;
  codeValue?: string;
  referenceTable?: string;
  referenceColumn?: string;
  displayColumn?: string; // 🎯 Entity 조인에서 표시할 컬럼명
  createdDate?: Date;
  updatedDate?: Date;
}

// 🎯 Entity 조인 관련 타입 정의
export interface EntityJoinConfig {
  sourceTable: string; // companies
  sourceColumn: string; // writer
  referenceTable: string; // user_info
  referenceColumn: string; // user_id (조인 키)
  displayColumns: string[]; // ['user_name', 'dept_name'] (표시할 값들)
  displayColumn?: string; // user_name (하위 호환성용, deprecated)
  aliasColumn: string; // writer_name (결과 컬럼명)
  separator?: string; // ' - ' (여러 컬럼 연결 시 구분자)
}

export interface EntityJoinResponse {
  data: Record<string, any>[];
  total: number;
  page: number;
  size: number;
  totalPages: number;
  entityJoinInfo?: {
    joinConfigs: EntityJoinConfig[];
    strategy: "full_join" | "cache_lookup" | "hybrid";
    performance: {
      queryTime: number;
      cacheHitRate?: number;
      hybridBreakdown?: {
        dbJoins: number;
        cacheJoins: number;
      };
    };
  };
}

export interface BatchLookupRequest {
  table: string;
  key: string;
  displayColumn: string;
}

export interface BatchLookupResponse {
  key: string;
  value: any;
}

// API 응답 타입
export interface TableListResponse {
  success: boolean;
  data: TableInfo[];
  message?: string;
  error?: {
    code: string;
    details?: any;
  };
}

export interface ColumnListResponse {
  success: boolean;
  data: ColumnTypeInfo[];
  message?: string;
  error?: {
    code: string;
    details?: any;
  };
}

export interface ColumnSettingsResponse {
  success: boolean;
  message?: string;
  error?: {
    code: string;
    details?: any;
  };
}

// 웹 타입 옵션
export const WEB_TYPE_OPTIONS = [
  { value: "text", label: "text", description: "일반 텍스트 입력" },
  { value: "number", label: "number", description: "숫자 입력" },
  { value: "date", label: "date", description: "날짜 선택기" },
  { value: "code", label: "code", description: "코드 선택 (공통코드 지정)" },
  {
    value: "entity",
    label: "entity",
    description: "엔티티 참조 (참조테이블 지정)",
  },
  { value: "textarea", label: "textarea", description: "여러 줄 텍스트" },
  { value: "select", label: "select", description: "드롭다운 선택" },
  { value: "checkbox", label: "checkbox", description: "체크박스" },
  { value: "radio", label: "radio", description: "라디오 버튼" },
  { value: "file", label: "file", description: "파일 업로드" },
] as const;

export type WebType = (typeof WEB_TYPE_OPTIONS)[number]["value"];
