export interface Task {
  id: string;
  name: string;
  description?: string;
  type: 'command' | 'function' | 'manual' | 'parallel' | 'sequential';
  status: 'pending' | 'running' | 'completed' | 'failed' | 'skipped' | 'cancelled';
  dependencies: string[]; // Task IDs that must complete before this task
  input?: any;
  output?: any;
  error?: string;
  retries: number;
  maxRetries: number;
  timeout?: number; // milliseconds
  startTime?: string;
  endTime?: string;
  duration?: number;
  metadata?: Record<string, any>;
}

export interface TaskCommand {
  command: string;
  workingDirectory?: string;
  environment?: Record<string, string>;
  shell?: string;
  captureOutput?: boolean;
}

export interface TaskFunction {
  name: string;
  module?: string;
  arguments?: any[];
  async?: boolean;
}

export interface TaskGroup {
  id: string;
  name: string;
  description?: string;
  tasks: string[]; // Task IDs
  type: 'parallel' | 'sequential';
  continueOnError?: boolean;
  maxConcurrency?: number; // For parallel groups
}

export interface Workflow {
  id: string;
  name: string;
  description?: string;
  version: string;
  tasks: Task[];
  groups: TaskGroup[];
  entryPoint: string; // Task or group ID
  variables: Record<string, any>;
  triggers?: WorkflowTrigger[];
  status: 'draft' | 'active' | 'completed' | 'failed' | 'cancelled';
  createdAt: string;
  updatedAt: string;
  completedAt?: string;
  author?: string;
  tags?: string[];
}

export interface WorkflowTrigger {
  type: 'schedule' | 'webhook' | 'file_change' | 'manual' | 'event';
  config: ScheduleTrigger | WebhookTrigger | FileChangeTrigger | EventTrigger;
  enabled: boolean;
}

export interface ScheduleTrigger {
  cron: string;
  timezone?: string;
}

export interface WebhookTrigger {
  url: string;
  secret?: string;
  method: 'GET' | 'POST';
  headers?: Record<string, string>;
}

export interface FileChangeTrigger {
  patterns: string[];
  events: ('created' | 'modified' | 'deleted')[];
  debounce?: number; // milliseconds
}

export interface EventTrigger {
  eventName: string;
  filter?: Record<string, any>;
}

export interface WorkflowExecution {
  id: string;
  workflowId: string;
  workflowVersion: string;
  status: 'pending' | 'running' | 'completed' | 'failed' | 'cancelled';
  trigger?: string;
  startTime: string;
  endTime?: string;
  duration?: number;
  tasks: TaskExecution[];
  variables: Record<string, any>;
  logs: ExecutionLog[];
  error?: string;
}

export interface TaskExecution {
  taskId: string;
  executionId: string;
  status: 'pending' | 'running' | 'completed' | 'failed' | 'skipped' | 'cancelled';
  startTime?: string;
  endTime?: string;
  duration?: number;
  input?: any;
  output?: any;
  error?: string;
  retryCount: number;
  logs: ExecutionLog[];
}

export interface ExecutionLog {
  timestamp: string;
  level: 'debug' | 'info' | 'warning' | 'error';
  taskId?: string;
  message: string;
  data?: any;
}

export interface TaskTemplate {
  id: string;
  name: string;
  description: string;
  category: 'development' | 'testing' | 'deployment' | 'maintenance' | 'custom';
  type: Task['type'];
  defaultConfig: Partial<Task>;
  parameters: TemplateParameter[];
  tags: string[];
}

export interface TemplateParameter {
  name: string;
  type: 'string' | 'number' | 'boolean' | 'array' | 'object';
  description?: string;
  required: boolean;
  default?: any;
  validation?: {
    pattern?: string;
    min?: number;
    max?: number;
    enum?: any[];
  };
}

export interface TaskDependency {
  taskId: string;
  dependsOn: string[];
  type: 'hard' | 'soft'; // Hard = must complete, Soft = should complete
  condition?: DependencyCondition;
}

export interface DependencyCondition {
  type: 'success' | 'failure' | 'any' | 'expression';
  expression?: string; // For complex conditions
}

export interface OrchestrationConfig {
  maxConcurrentTasks: number;
  defaultTimeout: number;
  retryPolicy: {
    maxRetries: number;
    backoffMultiplier: number;
    initialDelay: number;
    maxDelay: number;
  };
  executionMode: 'local' | 'distributed';
  persistence: {
    enabled: boolean;
    path: string;
  };
}

export interface TaskMetrics {
  totalExecutions: number;
  successfulExecutions: number;
  failedExecutions: number;
  averageDuration: number;
  lastExecution?: string;
  errorRate: number;
}

export interface WorkflowMetrics {
  totalRuns: number;
  successfulRuns: number;
  failedRuns: number;
  averageDuration: number;
  taskMetrics: Record<string, TaskMetrics>;
  popularTriggers: Record<string, number>;
}