// 배치관리 타입 정의
// 작성일: 2024-12-24

// 공통 API 응답 타입
export interface ApiResponse<T = any> {
  success: boolean;
  data?: T;
  message?: string;
  error?: string;
  pagination?: {
    page: number;
    limit: number;
    total: number;
    totalPages: number;
  };
}

// 컬럼 정보 타입
export interface ColumnInfo {
  column_name: string;
  data_type: string;
  is_nullable?: string;
  column_default?: string | null;
}

// 테이블 정보 타입
export interface TableInfo {
  table_name: string;
  table_type?: string;
  table_schema?: string;
}

// 연결 정보 타입
export interface ConnectionInfo {
  type: "internal" | "external";
  id?: number;
  name: string;
  db_type?: string;
}

// 배치 설정 필터 타입
export interface BatchConfigFilter {
  page?: number;
  limit?: number;
  search?: string;
  is_active?: string;
  company_code?: string;
}

// 배치 매핑 타입
export interface BatchMapping {
  id?: number;
  batch_config_id?: number;
  company_code?: string;
  from_connection_type: "internal" | "external" | "restapi";
  from_connection_id?: number;
  from_table_name: string;
  from_column_name: string;
  from_column_type?: string;
  from_api_url?: string;
  from_api_key?: string;
  from_api_method?: "GET" | "POST" | "PUT" | "DELETE";
  from_api_param_type?: "url" | "query";
  from_api_param_name?: string;
  from_api_param_value?: string;
  from_api_param_source?: "static" | "dynamic";
  from_api_body?: string;
  to_connection_type: "internal" | "external" | "restapi";
  to_connection_id?: number;
  to_table_name: string;
  to_column_name: string;
  to_column_type?: string;
  to_api_url?: string;
  to_api_key?: string;
  to_api_method?: "GET" | "POST" | "PUT" | "DELETE";
  to_api_body?: string;
  mapping_order?: number;
  created_by?: string;
  created_date?: Date;
}

// 배치 실행 타입: 기존 매핑 방식 또는 노드 플로우 실행
export type BatchExecutionType = "mapping" | "node_flow";

// 배치 설정 타입
export interface BatchConfig {
  id?: number;
  batch_name: string;
  description?: string;
  cron_schedule: string;
  is_active: "Y" | "N";
  company_code?: string;
  save_mode?: "INSERT" | "UPSERT";
  conflict_key?: string;
  auth_service_name?: string;
  data_array_path?: string;
  execution_type?: BatchExecutionType;
  node_flow_id?: number;
  node_flow_context?: Record<string, any>;
  created_by?: string;
  created_date?: Date;
  updated_by?: string;
  updated_date?: Date;
  batch_mappings?: BatchMapping[];
  last_status?: string;
  last_executed_at?: string;
  last_total_records?: number;
}

export interface BatchConnectionInfo {
  type: "internal" | "external";
  id?: number;
  name: string;
  db_type?: string;
}

export interface BatchColumnInfo {
  column_name: string;
  data_type: string;
  is_nullable?: string;
  column_default?: string | null;
}

export interface BatchMappingRequest {
  from_connection_type: "internal" | "external" | "restapi" | "fixed";
  from_connection_id?: number;
  from_table_name: string;
  from_column_name: string;
  from_column_type?: string;
  from_api_url?: string;
  from_api_key?: string;
  from_api_method?: "GET" | "POST" | "PUT" | "DELETE";
  from_api_param_type?: "url" | "query"; // API 파라미터 타입
  from_api_param_name?: string; // API 파라미터명
  from_api_param_value?: string; // API 파라미터 값 또는 템플릿
  from_api_param_source?: "static" | "dynamic"; // 파라미터 소스 타입
  // REST API Body 추가 (FROM - REST API에서 POST 요청 시 필요)
  from_api_body?: string;
  to_connection_type: "internal" | "external" | "restapi";
  to_connection_id?: number;
  to_table_name: string;
  to_column_name: string;
  to_column_type?: string;
  to_api_url?: string;
  to_api_key?: string;
  to_api_method?: "GET" | "POST" | "PUT" | "DELETE";
  to_api_body?: string; // Request Body 템플릿 (DB → REST API 배치용)
  mapping_order?: number;
  mapping_type?: "direct" | "fixed"; // 매핑 타입: direct (API 필드) 또는 fixed (고정값)
}

export interface CreateBatchConfigRequest {
  batchName: string;
  description?: string;
  cronSchedule: string;
  isActive: "Y" | "N";
  companyCode: string;
  saveMode?: "INSERT" | "UPSERT";
  conflictKey?: string;
  authServiceName?: string;
  dataArrayPath?: string;
  executionType?: BatchExecutionType;
  nodeFlowId?: number;
  nodeFlowContext?: Record<string, any>;
  mappings: BatchMappingRequest[];
}

export interface UpdateBatchConfigRequest {
  batchName?: string;
  description?: string;
  cronSchedule?: string;
  isActive?: "Y" | "N";
  saveMode?: "INSERT" | "UPSERT";
  conflictKey?: string;
  authServiceName?: string;
  dataArrayPath?: string;
  executionType?: BatchExecutionType;
  nodeFlowId?: number;
  nodeFlowContext?: Record<string, any>;
  mappings?: BatchMappingRequest[];
}

export interface BatchValidationResult {
  isValid: boolean;
  errors: string[];
}
