/**
 * @fileoverview Core type definitions for the workflow engine
 */

// MARK: 节点状态
export const NodeStatus = {
  PENDING: 'PENDING',      // Waiting for execution
  RUNNING: 'RUNNING',      // Currently executing
  COMPLETED: 'COMPLETED',  // Successfully completed
  FAILED: 'FAILED',        // Execution failed
  TIMEOUT: 'TIMEOUT',      // Execution timed out
  SKIPPED: 'SKIPPED',      // Step was skipped
  CANCELLED: 'CANCELLED',  // Execution was cancelled
  COMPENSATING: 'COMPENSATING', // Compensation in progress
  COMPENSATED: 'COMPENSATED',   // Compensation completed
} as const;

// MARK: 步骤类型
export const StepType = {
  TASK: 'TASK',              // 常规任务节点
  CONDITION: 'CONDITION',    // 条件分支
  PARALLEL: 'PARALLEL',      // 并行执行
  SUB_WORKFLOW: 'SUB_WORKFLOW', // 子工作流
  EVENT_TRIGGER: 'EVENT_TRIGGER', // 事件触发
  EVENT_LISTENER: 'EVENT_LISTENER', // 事件监听
} as const;

// MARK: 类型导出
export type NodeStatus = typeof NodeStatus[keyof typeof NodeStatus];
export type StepType = typeof StepType[keyof typeof StepType];

// MARK: 工作流上下文
export interface IWorkflowContext {
  workflowId: string;
  workflowDefinitionId: string;
  input: Record<string, any>;
  output?: Record<string, any>;
  status: NodeStatus;
  variables: Record<string, any>;
  startTime?: Date;
  endTime?: Date;
  tenantId?: string;
  userId?: string;
  userRole?: string;
  history: IExecutionRecord[];
  [key: string]: any;
}

// MARK: 节点上下文
export interface INodeContext {
  nodeId: string;
  stepId: string;
  input: Record<string, any>;
  output?: Record<string, any>;
  status: NodeStatus;
  startTime?: Date;
  endTime?: Date;
  retries: number;
  error?: Error;
  logs: string[];
  workflowContext: IWorkflowContext;
  [key: string]: any;
}

// MARK: 节点输出
export interface INodeOutput {
  status: NodeStatus;
  output?: Record<string, any>;
  error?: Error;
}

// MARK: 节点
export interface INode {
  id: string;
  execute(context: INodeContext): Promise<INodeOutput>;
  compensate?(context: INodeContext): Promise<void>;
  validateInput?(input: Record<string, any>): boolean;
  requiredRoles?: string[];
}

// MARK: 步骤配置
export interface IStepConfig {
  id: string;
  type: StepType;
  nodeId?: string;
  name?: string;
  input?: Record<string, any>;
  outputMapping?: Record<string, string>;
  nextStepId?: string;
  timeout?: number;
  maxRetries?: number;
  retryDelay?: number;
  condition?: (context: IWorkflowContext) => boolean | string;
  branches?: { [key: string]: string };
  parallelSteps?: IStepConfig[];
  subWorkflowId?: string;
  subWorkflowInput?: Record<string, any>;
  event?: string;
  sla?: number;
  compensateOnFailure?: boolean | string;
  requiredRoles?: string[];
}

// MARK: 执行记录
export interface IExecutionRecord {
  stepId: string;
  nodeId?: string;
  status: NodeStatus;
  startTime: Date;
  endTime?: Date;
  input: Record<string, any>;
  output?: Record<string, any>;
  error?: string;
  logs: string[];
  attempt: number;
}

// MARK: 事件负载
export interface IEventPayload {
  eventName: string;
  data?: Record<string, any>;
  timestamp: Date;
  source?: string;
}