/**
 * 플로우 관리 시스템 - 프론트엔드 타입 정의
 */

// ============================================
// 조건 연산자
// ============================================
export type ConditionOperator =
  | "equals"
  | "not_equals"
  | "in"
  | "not_in"
  | "greater_than"
  | "less_than"
  | "greater_than_or_equal"
  | "less_than_or_equal"
  | "is_null"
  | "is_not_null"
  | "like"
  | "not_like";

// ============================================
// 플로우 조건
// ============================================
export interface FlowCondition {
  column: string;
  operator: ConditionOperator;
  value?: any;
}

export interface FlowConditionGroup {
  type: "AND" | "OR";
  conditions: FlowCondition[];
}

// ============================================
// 다중 REST API 연결 설정
// ============================================
export interface RestApiConnectionConfig {
  connectionId: number;
  connectionName: string;
  endpoint: string;
  jsonPath: string;
  alias: string; // 컬럼 접두어 (예: "api1_")
}

// ============================================
// 다중 외부 DB 연결 설정
// ============================================
export interface ExternalDbConnectionConfig {
  connectionId: number;
  connectionName: string;
  dbType: string;
  tableName: string;
  alias: string; // 컬럼 접두어 (예: "db1_")
}

// ============================================
// 플로우 정의
// ============================================
export interface FlowDefinition {
  id: number;
  name: string;
  description?: string;
  tableName: string;
  // 데이터 소스 관련
  dbSourceType?: "internal" | "external" | "restapi" | "multi_restapi" | "multi_external_db";
  dbConnectionId?: number;
  // REST API 관련 (단일)
  restApiConnectionId?: number;
  restApiEndpoint?: string;
  restApiJsonPath?: string;
  // 다중 REST API 관련
  restApiConnections?: RestApiConnectionConfig[];
  // 다중 외부 DB 관련
  externalDbConnections?: ExternalDbConnectionConfig[];
  isActive: boolean;
  createdAt: string;
  updatedAt: string;
  createdBy: string;
  updatedBy: string;
}

export interface CreateFlowDefinitionRequest {
  name: string;
  description?: string;
  tableName: string;
  // 데이터 소스 관련
  dbSourceType?: "internal" | "external" | "restapi" | "multi_restapi" | "multi_external_db";
  dbConnectionId?: number;
  // REST API 관련 (단일)
  restApiConnectionId?: number;
  restApiEndpoint?: string;
  restApiJsonPath?: string;
  // 다중 REST API 관련
  restApiConnections?: RestApiConnectionConfig[];
  // 다중 외부 DB 관련
  externalDbConnections?: ExternalDbConnectionConfig[];
}

export interface UpdateFlowDefinitionRequest {
  name?: string;
  description?: string;
  tableName?: string;
  isActive?: boolean;
}

// ============================================
// 플로우 단계 표시 설정
// ============================================
export interface FlowStepDisplayConfig {
  visibleColumns?: string[]; // 표시할 컬럼 목록
  columnOrder?: string[]; // 컬럼 순서 (선택사항)
  columnLabels?: Record<string, string>; // 컬럼별 커스텀 라벨 (선택사항)
  columnWidths?: Record<string, number>; // 컬럼별 너비 설정 (px, 선택사항)
}

// ============================================
// 플로우 단계
// ============================================
export interface FlowStep {
  id: number;
  flowDefinitionId: number;
  stepName: string;
  stepOrder: number;
  tableName?: string; // 이 단계에서 조회할 테이블명
  conditionJson?: FlowConditionGroup;
  color: string;
  positionX: number;
  positionY: number;
  // 🆕 표시 설정 (플로우 위젯에서 사용)
  displayConfig?: FlowStepDisplayConfig; // 단계별 컬럼 표시 설정
  createdAt: string;
  updatedAt: string;
  createdBy: string;
  updatedBy: string;
}

export interface CreateFlowStepRequest {
  stepName: string;
  stepOrder: number;
  tableName?: string; // 이 단계에서 조회할 테이블명
  conditionJson?: FlowConditionGroup;
  color?: string;
  positionX?: number;
  positionY?: number;
  displayConfig?: FlowStepDisplayConfig; // 🆕 표시 설정
}

export interface UpdateFlowStepRequest {
  stepName?: string;
  tableName?: string; // 이 단계에서 조회할 테이블명
  stepOrder?: number;
  conditionJson?: FlowConditionGroup;
  color?: string;
  positionX?: number;
  positionY?: number;
  displayConfig?: FlowStepDisplayConfig; // 🆕 표시 설정
}

// ============================================
// 플로우 단계 연결
// ============================================
export interface FlowStepConnection {
  id: number;
  flowDefinitionId: number;
  fromStepId: number;
  toStepId: number;
  label?: string;
  createdAt: string;
  updatedAt: string;
}

export interface CreateFlowConnectionRequest {
  flowDefinitionId: number;
  fromStepId: number;
  toStepId: number;
  label?: string;
}

// ============================================
// 플로우 데이터 상태
// ============================================
export interface FlowDataStatus {
  id: number;
  flowDefinitionId: number;
  tableName: string;
  recordId: string;
  currentStepId: number | null;
  updatedAt: string;
  updatedBy: string;
}

// ============================================
// 플로우 오딧 로그
// ============================================
export interface FlowAuditLog {
  id: number;
  flowDefinitionId: number;
  tableName: string;
  recordId: string;
  fromStepId: number | null;
  toStepId: number | null;
  changedAt: string;
  changedBy: string;
  note?: string;
  fromStepName?: string;
  toStepName?: string;
  moveType?: "status" | "table" | "both";
  sourceTable?: string;
  targetTable?: string;
  sourceDataId?: string;
  targetDataId?: string;
  statusFrom?: string;
  statusTo?: string;
  dbConnectionId?: number;
  dbConnectionName?: string;
}

// ============================================
// 플로우 실행 관련
// ============================================
export interface FlowStepDataCount {
  stepId: number;
  stepName: string;
  count: number;
}

export interface FlowStepDataList {
  records: any[];
  total: number;
  page: number;
  pageSize: number;
}

export interface MoveDataRequest {
  flowId: number;
  fromStepId: number;
  recordId: string;
  toStepId: number;
  note?: string;
}

export interface MoveBatchDataRequest {
  flowId: number;
  fromStepId: number;
  toStepId: number;
  dataIds: string[];
  note?: string;
}

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

// ============================================
// React Flow 관련 타입 (비주얼 편집기용)
// ============================================
export interface FlowNodeData {
  id: number;
  label: string;
  stepOrder: number;
  tableName?: string;
  count?: number;
  condition?: FlowConditionGroup;
  integrationType?: string;
  procedureName?: string;
}

export interface FlowEdgeData {
  id: number;
  label?: string;
}

// ============================================
// UI 컴포넌트용 타입
// ============================================
export interface FlowListItem extends FlowDefinition {
  stepsCount?: number;
  lastUpdated?: string;
}

export interface FlowEditorState {
  flowDefinition: FlowDefinition | null;
  steps: FlowStep[];
  connections: FlowStepConnection[];
  selectedStepId: number | null;
  isEditMode: boolean;
}
