// 화면관리 시스템 타입 정의

// 기본 컴포넌트 타입
export type ComponentType = "container" | "row" | "column" | "widget" | "group";

// 웹 타입 정의
// WebType은 통합 타입에서 import (중복 정의 제거)
import { WebType } from "./v2-web-types";
export { WebType };

// 위치 정보
export interface Position {
  x: number;
  y: number;
  z?: number; // z-index (레이어 순서)
}

// 크기 정보
export interface Size {
  width: number; // 1-12 그리드
  height: number; // 픽셀
}

// 기본 컴포넌트 인터페이스
export interface BaseComponent {
  id: string;
  type: ComponentType;
  position: Position;
  size: Size;
  properties?: Record<string, any>;
  displayOrder?: number;
  parentId?: string; // 부모 컴포넌트 ID 추가
}

// 컨테이너 컴포넌트
export interface ContainerComponent extends BaseComponent {
  type: "container";
  backgroundColor?: string;
  border?: string;
  borderRadius?: number;
  shadow?: string;
  padding?: number;
  margin?: number;
}

// 그룹 컴포넌트
export interface GroupComponent extends BaseComponent {
  type: "group";
  title?: string;
  backgroundColor?: string;
  border?: string;
  borderRadius?: number;
  shadow?: string;
  padding?: number;
  margin?: number;
  collapsible?: boolean;
  collapsed?: boolean;
  children: string[]; // 포함된 컴포넌트 ID 목록
}

// 행 컴포넌트
export interface RowComponent extends BaseComponent {
  type: "row";
  columns: number; // 1-12
  gap: number;
  alignItems: "start" | "center" | "end";
  justifyContent: "start" | "center" | "end" | "space-between";
}

// 컬럼 컴포넌트
export interface ColumnComponent extends BaseComponent {
  type: "column";
  offset?: number;
  order?: number;
}

// 위젯 컴포넌트
export interface WidgetComponent extends BaseComponent {
  type: "widget";
  tableName: string;
  columnName: string;
  widgetType: WebType;
  label: string;
  placeholder?: string;
  required: boolean;
  readonly: boolean;
  validationRules?: ValidationRule[];
  displayProperties?: Record<string, any>;
}

// 컴포넌트 유니온 타입
export type ComponentData =
  | ContainerComponent
  | GroupComponent
  | RowComponent
  | ColumnComponent
  | WidgetComponent;

// 레이아웃 데이터
export interface LayoutData {
  components: ComponentData[];
  gridSettings?: GridSettings;
  screenResolution?: ScreenResolution;
  tableName?: string; // 🆕 화면에 연결된 테이블명
}

// 그리드 설정
export interface GridSettings {
  columns: number; // 기본값: 12
  gap: number; // 기본값: 16px
  padding: number; // 기본값: 16px
  snapToGrid?: boolean; // 격자에 맞춤 여부 (기본값: true)
  showGrid?: boolean; // 격자 표시 여부 (기본값: true)
  gridColor?: string; // 격자 색상 (기본값: #d1d5db)
  gridOpacity?: number; // 격자 투명도 (기본값: 0.5)
}

// 화면 해상도 설정
export interface ScreenResolution {
  width: number;
  height: number;
  name: string;
  category: "desktop" | "tablet" | "mobile" | "custom";
}

// 유효성 검증 규칙
export interface ValidationRule {
  type:
    | "required"
    | "minLength"
    | "maxLength"
    | "pattern"
    | "min"
    | "max"
    | "email"
    | "url";
  value?: any;
  message: string;
}

// 화면 정의
export interface ScreenDefinition {
  screenId: number;
  screenName: string;
  screenCode: string;
  tableName: string;
  tableLabel?: string; // 테이블 라벨 (한글명)
  companyCode: string;
  description?: string;
  isActive: string;
  createdDate: Date;
  createdBy?: string;
  updatedDate: Date;
  updatedBy?: string;
  dbSourceType?: "internal" | "external";
  dbConnectionId?: number;
  // REST API 관련 필드
  dataSourceType?: "database" | "restapi";
  restApiConnectionId?: number;
  restApiEndpoint?: string;
  restApiJsonPath?: string;
}

// 화면 생성 요청
export interface CreateScreenRequest {
  screenName: string;
  screenCode: string;
  tableName: string;
  companyCode: string;
  description?: string;
  createdBy?: string;
  dbSourceType?: "internal" | "external";
  dbConnectionId?: number;
  // REST API 관련 필드
  dataSourceType?: "database" | "restapi";
  restApiConnectionId?: number;
  restApiEndpoint?: string;
  restApiJsonPath?: string;
}

// 화면 수정 요청
export interface UpdateScreenRequest {
  screenName?: string;
  description?: string;
  isActive?: string;
  updatedBy?: string;
}

// 레이아웃 저장 요청
export interface SaveLayoutRequest {
  components: ComponentData[];
  gridSettings?: GridSettings;
}

// 화면 템플릿
export interface ScreenTemplate {
  templateId: number;
  templateName: string;
  templateType: string;
  companyCode: string;
  description?: string;
  layoutData?: LayoutData;
  isPublic: boolean;
  createdBy?: string;
  createdDate: Date;
}

// 메뉴 할당 요청
export interface MenuAssignmentRequest {
  menuObjid: number;
  companyCode: string;
  displayOrder?: number;
  createdBy?: string;
}

// 드래그 상태
export interface DragState {
  isDragging: boolean;
  draggedItem: ComponentData | null;
  dragSource: "toolbox" | "canvas";
  dropTarget: string | null;
  dropZone?: DropZone;
}

// 드롭 영역
export interface DropZone {
  id: string;
  accepts: ComponentType[];
  position: Position;
  size: Size;
}

// 그룹화 상태
export interface GroupState {
  isGrouping: boolean;
  selectedComponents: string[];
  groupTarget: string | null;
  groupMode: "create" | "add" | "remove";
}

// 컬럼 정보 (테이블 타입관리 연계용)
export interface ColumnInfo {
  tableName: string;
  columnName: string;
  columnLabel?: string;
  dataType: string;
  webType?: WebType;
  inputType?: "direct" | "auto"; // 입력 타입
  isNullable: string;
  columnDefault?: string;
  characterMaximumLength?: number;
  numericPrecision?: number;
  numericScale?: number;
  detailSettings?: string; // JSON 문자열
  codeCategory?: string;
  referenceTable?: string;
  referenceColumn?: string;
  isVisible?: boolean;
  displayOrder?: number;
  description?: string;
}

// 웹 타입 설정
export interface ColumnWebTypeSetting {
  tableName: string;
  columnName: string;
  webType: WebType;
  columnLabel?: string;
  detailSettings?: Record<string, any>;
  codeCategory?: string;
  referenceTable?: string;
  referenceColumn?: string;
  isVisible?: boolean;
  displayOrder?: number;
  description?: string;
}

// 위젯 데이터
export interface WidgetData {
  id: string;
  tableName: string;
  columnName: string;
  type: WebType;
  label: string;
  required: boolean;
  readonly: boolean;
  [key: string]: any; // 추가 속성들
}

// API 응답 타입
export interface ApiResponse<T> {
  success: boolean;
  data?: T;
  message?: string;
  errorCode?: string;
}

// 페이지네이션 응답
export interface PaginatedResponse<T> {
  data: T[];
  pagination: {
    total: number;
    page: number;
    size: number;
    totalPages: number;
  };
}
