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

// ============================================
// 조건 연산자
// ============================================
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 {
  connection_id: number;
  connection_name: string;
  endpoint: string;
  json_path: string;
  alias: string; // 컬럼 접두어 (예: "api1_")
}

// ============================================
// 다중 외부 DB 연결 설정
// ============================================
export interface ExternalDbConnectionConfig {
  connection_id: number;
  connection_name: string;
  db_type: string;
  table_name: string;
  alias: string; // 컬럼 접두어 (예: "db1_")
}

// ============================================
// 플로우 정의
// ============================================
export interface FlowDefinition {
  id: number;
  name: string;
  description?: string;
  table_name: string;
  // 데이터 소스 관련
  db_source_type?: "internal" | "external" | "restapi" | "multi_restapi" | "multi_external_db";
  db_connection_id?: number;
  // REST API 관련 (단일)
  rest_api_connection_id?: number;
  rest_api_endpoint?: string;
  rest_api_json_path?: string;
  // 다중 REST API 관련
  rest_api_connections?: RestApiConnectionConfig[];
  // 다중 외부 DB 관련
  external_db_connections?: ExternalDbConnectionConfig[];
  is_active: boolean;
  created_at: string;
  updated_at: string;
  created_by: string;
  updated_by: string;
}

export interface CreateFlowDefinitionRequest {
  name: string;
  description?: string;
  table_name: string;
  // 데이터 소스 관련
  db_source_type?: "internal" | "external" | "restapi" | "multi_restapi" | "multi_external_db";
  db_connection_id?: number;
  // REST API 관련 (단일)
  rest_api_connection_id?: number;
  rest_api_endpoint?: string;
  rest_api_json_path?: string;
  // 다중 REST API 관련
  rest_api_connections?: RestApiConnectionConfig[];
  // 다중 외부 DB 관련
  external_db_connections?: ExternalDbConnectionConfig[];
}

export interface UpdateFlowDefinitionRequest {
  name?: string;
  description?: string;
  table_name?: string;
  is_active?: boolean;
}

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

// ============================================
// 플로우 단계
// ============================================
export interface FlowStep {
  id: number;
  flow_definition_id: number;
  step_name: string;
  step_order: number;
  table_name?: string; // 이 단계에서 조회할 테이블명
  condition_json?: FlowConditionGroup;
  color: string;
  position_x: number;
  position_y: number;
  // 🆕 표시 설정 (플로우 위젯에서 사용)
  display_config?: FlowStepDisplayConfig; // 단계별 컬럼 표시 설정
  created_at: string;
  updated_at: string;
  created_by: string;
  updated_by: string;
}

export interface CreateFlowStepRequest {
  step_name: string;
  step_order: number;
  table_name?: string; // 이 단계에서 조회할 테이블명
  condition_json?: FlowConditionGroup;
  color?: string;
  position_x?: number;
  position_y?: number;
  display_config?: FlowStepDisplayConfig; // 🆕 표시 설정
}

export interface UpdateFlowStepRequest {
  step_name?: string;
  table_name?: string; // 이 단계에서 조회할 테이블명
  step_order?: number;
  condition_json?: FlowConditionGroup;
  color?: string;
  position_x?: number;
  position_y?: number;
  display_config?: FlowStepDisplayConfig; // 🆕 표시 설정
}

// ============================================
// 플로우 단계 연결
// ============================================
export interface FlowStepConnection {
  id: number;
  flow_definition_id: number;
  from_step_id: number;
  to_step_id: number;
  label?: string;
  created_at: string;
  updated_at: string;
}

export interface CreateFlowConnectionRequest {
  flow_definition_id: number;
  from_step_id: number;
  to_step_id: number;
  label?: string;
}

// ============================================
// 플로우 데이터 상태
// ============================================
export interface FlowDataStatus {
  id: number;
  flow_definition_id: number;
  table_name: string;
  record_id: string;
  current_step_id: number | null;
  updated_at: string;
  updated_by: string;
}

// ============================================
// 플로우 오딧 로그
// ============================================
export interface FlowAuditLog {
  id: number;
  flow_definition_id: number;
  table_name: string;
  record_id: string;
  from_step_id: number | null;
  to_step_id: number | null;
  changed_at: string;
  changed_by: string;
  note?: string;
  from_step_name?: string;
  to_step_name?: string;
  move_type?: "status" | "table" | "both";
  source_table?: string;
  target_table?: string;
  source_data_id?: string;
  target_data_id?: string;
  status_from?: string;
  status_to?: string;
  db_connection_id?: number;
  db_connection_name?: string;
}

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

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

export interface MoveDataRequest {
  flow_id: number;
  from_step_id: number;
  record_id: string;
  to_step_id: number;
  note?: string;
}

export interface MoveBatchDataRequest {
  flow_id: number;
  from_step_id: number;
  to_step_id: number;
  data_ids: 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;
  step_order: number;
  table_name?: string;
  count?: number;
  condition?: FlowConditionGroup;
  integration_type?: string;
  procedure_name?: string;
}

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

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

export interface FlowEditorState {
  flow_definition: FlowDefinition | null;
  steps: FlowStep[];
  connections: FlowStepConnection[];
  selected_step_id: number | null;
  is_edit_mode: boolean;
}
