/**
 * 工作流相关的类型定义
 * 
 * 统一管理工作流系统中使用的所有类型定义
 */

// 工作流类型枚举
export type WorkflowType = 'outfit_generation' | 'background_replacement' | 'portrait_enhancement' | 'image_upscaling' | 'style_transfer' | 'custom';

// 执行状态枚举
export type ExecutionStatus = 'pending' | 'running' | 'completed' | 'failed' | 'cancelled';

// 环境类型枚举
export type EnvironmentType = 'local_comfyui' | 'modal_cloud' | 'runpod_cloud' | 'custom';

// 健康状态枚举
export type HealthStatus = 'healthy' | 'unhealthy' | 'unknown';

// UI字段类型枚举
export type UIFieldType = 'text' | 'textarea' | 'number' | 'select' | 'checkbox' | 'image_upload' | 'file_upload' | 'slider' | 'color' | 'date';

// 节点映射配置接口
export interface NodeMapping {
  /** 目标节点ID */
  node_id: string;
  /** 目标输入字段名 */
  input_field: string;
  /** 数据转换类型 */
  transform_type?: 'string' | 'number' | 'boolean' | 'array' | 'object' | 'file_url';
  /** 默认值（当UI字段为空时使用） */
  default_value?: any;
  /** 是否必需（如果UI字段为空且没有默认值，则报错） */
  required?: boolean;
}

// UI字段配置接口
export interface UIField {
  name: string;
  type: UIFieldType;
  label: string;
  description?: string;
  required?: boolean;
  default_value?: any;
  validation?: {
    min?: number;
    max?: number;
    step?: number;
    pattern?: string;
    options?: string[];
  };
  ui_config?: {
    placeholder?: string;
    help_text?: string;
    group?: string;
    order?: number;
    width?: 'full' | 'half' | 'third';
  };
  /** 节点映射配置 */
  node_mapping?: NodeMapping;
}

// 工作流模板接口
export interface WorkflowTemplate {
  id: number;
  name: string;
  base_name: string;
  version: string;
  type: WorkflowType;
  description?: string;
  comfyui_workflow_json: any;
  ui_config_json: {
    form_fields: UIField[];
    layout?: any;
    theme?: string;
  };
  execution_config_json?: any;
  input_schema_json?: any;
  output_schema_json?: any;
  is_active: boolean;
  is_published: boolean;
  category?: string;
  author?: string;
  tags?: string[];
  created_at: string;
  updated_at: string;
}

// 创建工作流请求接口
export interface CreateWorkflowTemplateRequest {
  name: string;
  base_name: string;
  version: string;
  type: WorkflowType;
  description?: string;
  comfyui_workflow_json: any;
  ui_config_json: {
    form_fields: UIField[];
    layout?: any;
    theme?: string;
  };
  execution_config_json?: any;
  input_schema_json?: any;
  output_schema_json?: any;
  tags?: string[];
  category?: string;
  author?: string;
}

// 工作流执行请求接口
export interface ExecuteWorkflowRequest {
  /** 工作流模板ID */
  workflow_template_id: number;
  /** 用户输入的表单数据 */
  input_data: Record<string, any>;
  /** 执行环境ID（可选，如果不指定则使用默认环境） */
  execution_environment_id?: number;
  /** 执行配置覆盖（可选） */
  execution_config_override?: any;
}

// 工作流执行响应接口
export interface ExecuteWorkflowResponse {
  /** 执行记录ID */
  execution_id: number;
  /** 执行状态 */
  status: ExecutionStatus;
  /** ComfyUI提示ID */
  comfyui_prompt_id?: string;
  /** 错误信息 */
  error_message?: string;
  /** 执行进度 (0-100) */
  progress?: number;
  /** 输出数据 */
  output_data?: any[];
}

// 执行记录接口
export interface WorkflowExecutionRecord {
  id: number;
  workflow_template_id: number;
  workflow_name: string;
  workflow_version: string;
  execution_environment_id?: number;
  execution_environment_name?: string;
  input_data_json: any;
  output_data_json?: any;
  status: ExecutionStatus;
  progress: number;
  comfyui_prompt_id?: string;
  comfyui_workflow_name?: string;
  started_at?: string;
  completed_at?: string;
  duration_seconds?: number;
  error_message?: string;
  error_details_json?: any;
  user_id?: string;
  session_id?: string;
  metadata_json?: any;
  tags?: string[];
  created_at: string;
  updated_at: string;
}

// 执行历史分页响应接口
export interface ExecutionHistoryResponse {
  records: WorkflowExecutionRecord[];
  total_count: number;
  page: number;
  page_size: number;
  total_pages: number;
}

// 执行环境接口
export interface WorkflowExecutionEnvironment {
  id: number;
  name: string;
  environment_type: EnvironmentType;
  description?: string;
  base_url: string;
  api_key?: string;
  connection_config_json?: any;
  supported_workflow_types: string[];
  max_concurrent_jobs: number;
  priority: number;
  is_active: boolean;
  is_available: boolean;
  last_health_check?: string;
  health_status: HealthStatus;
  average_response_time_ms?: number;
  success_rate: number;
  total_executions: number;
  failed_executions: number;
  max_memory_mb?: number;
  max_execution_time_seconds?: number;
  metadata_json?: any;
  tags?: string[];
  created_at: string;
  updated_at: string;
}

// 创建环境请求接口
export interface CreateEnvironmentRequest {
  name: string;
  environment_type: EnvironmentType;
  description?: string;
  base_url: string;
  api_key?: string;
  connection_config_json?: any;
  supported_workflow_types: string[];
  max_concurrent_jobs?: number;
  priority?: number;
  max_memory_mb?: number;
  max_execution_time_seconds?: number;
  metadata_json?: any;
  tags?: string[];
}

// 执行记录筛选器接口
export interface ExecutionRecordFilter {
  workflow_template_id?: number;
  status?: ExecutionStatus;
  user_id?: string;
  session_id?: string;
  comfyui_prompt_id?: string;
  date_from?: string;
  date_to?: string;
}

// 环境筛选器接口
export interface EnvironmentFilter {
  environment_type?: EnvironmentType;
  is_active?: boolean;
  is_available?: boolean;
  health_status?: HealthStatus;
}

// 系统统计信息接口
export interface SystemStatistics {
  execution_stats: {
    total_executions_today: number;
    successful_executions_today: number;
    failed_executions_today: number;
    average_execution_time_seconds: number;
    current_queue_size: number;
    active_executions: number;
  };
  
  environment_stats: {
    total_environments: number;
    healthy_environments: number;
    unhealthy_environments: number;
    average_response_time_ms: number;
    total_capacity: number;
    used_capacity: number;
  };
  
  system_resources: {
    cpu_usage_percent: number;
    memory_usage_percent: number;
    disk_usage_percent: number;
    network_status: 'connected' | 'disconnected' | 'limited';
  };
  
  performance_metrics: {
    requests_per_minute: number;
    average_response_time_ms: number;
    error_rate_percent: number;
    uptime_hours: number;
  };
}
