export type PersonaType = 
  | 'software-engineer'
  | 'architect' 
  | 'cto'
  | 'ceo'
  | 'cfo'
  | 'marketing'
  | 'sales'
  | 'product-manager'
  | 'designer'
  | 'custom';

export type TriggerType = 
  | 'manual'
  | 'schedule'
  | 'event'
  | 'webhook'
  | 'condition'
  | 'chain';

export type ActivityType = 
  | 'tool'
  | 'human'
  | 'agent'
  | 'conditional'
  | 'loop'
  | 'parallel'
  | 'external';

export interface ProcessTrigger {
  id: string;
  type: TriggerType;
  name: string;
  enabled: boolean;
  config: TriggerConfig;
  aiSuggested?: boolean;
  reasoning?: string;
}

export interface TriggerConfig {
  // Schedule trigger
  cron?: string;
  timezone?: string;
  
  // Event trigger
  event?: string;
  source?: string;
  filters?: Record<string, any>;
  
  // Webhook trigger
  endpoint?: string;
  secret?: string;
  
  // Condition trigger
  condition?: string;
  checkInterval?: number;
  
  // Chain trigger
  parentProcessId?: string;
  parentStatus?: 'success' | 'failure' | 'any';
  
  // Common
  delay?: number; // ms to wait before execution
  timeout?: number;
  retryPolicy?: RetryPolicy;
}

export interface RetryPolicy {
  maxAttempts: number;
  backoffStrategy: 'linear' | 'exponential';
  initialDelay: number;
  maxDelay: number;
}

export interface Activity {
  id: string;
  type: ActivityType;
  name: string;
  description?: string;
  config: ActivityConfig;
  inputs?: Record<string, any>;
  outputs?: string[]; // Variable names to store outputs
  errorHandler?: ErrorHandler;
  condition?: string; // Only run if condition is true
}

export interface ActivityConfig {
  // Tool activity
  toolName?: string;
  toolArgs?: Record<string, any>;
  
  // Human activity  
  assignTo?: string[];
  prompt?: string;
  formFields?: FormField[];
  approvalType?: 'any' | 'all' | 'specific';
  timeout?: number;
  
  // Agent activity
  agentType?: string;
  agentTask?: string;
  agentCapabilities?: string[];
  agentConfig?: Record<string, any>;
  priority?: 'low' | 'medium' | 'high' | 'critical';
  processContext?: Record<string, any>;
  
  // Conditional activity
  conditions?: ConditionalBranch[];
  defaultBranch?: Activity[];
  
  // Loop activity
  collection?: string; // Variable name or expression
  itemVariable?: string;
  activities?: Activity[];
  maxIterations?: number;
  
  // Parallel activity
  branches?: Activity[][];
  waitForAll?: boolean;
  
  // External activity
  url?: string;
  method?: 'GET' | 'POST' | 'PUT' | 'DELETE';
  headers?: Record<string, string>;
  body?: any;
  authentication?: AuthConfig;
}

export interface FormField {
  name: string;
  type: 'text' | 'number' | 'boolean' | 'select' | 'multiselect' | 'date';
  label: string;
  required?: boolean;
  defaultValue?: any;
  options?: string[]; // For select/multiselect
  validation?: string; // Regex or expression
}

export interface AgentActivityConfig {
  agentType: string; // researcher, coder, reviewer, tester, documenter, analyzer, etc.
  agentTask: string; // Description of what the agent should do
  agentCapabilities?: string[]; // Required capabilities (e.g., ['research', 'analysis'])
  priority?: 'low' | 'medium' | 'high' | 'critical';
  processContext?: Record<string, any>; // Additional context for the agent
  timeout?: number; // Max execution time in milliseconds
  retryOnFailure?: boolean; // Whether to retry if agent fails
  fallbackToSimulation?: boolean; // Whether to use simulation as fallback
}

export interface ConditionalBranch {
  condition: string;
  activities: Activity[];
}

export interface ErrorHandler {
  type: 'retry' | 'skip' | 'fail' | 'alternate';
  retryPolicy?: RetryPolicy;
  alternateActivities?: Activity[];
  notifyOn?: 'always' | 'final-failure';
}

export interface AuthConfig {
  type: 'bearer' | 'basic' | 'oauth2' | 'api-key';
  credentials: Record<string, string>;
}

export interface ProcessDefinition {
  id: string;
  name: string;
  description?: string;
  version: string;
  persona?: PersonaType;
  category?: string;
  tags?: string[];
  
  triggers: ProcessTrigger[];
  activities: Activity[];
  variables?: Record<string, any>;
  
  onSuccess?: Activity[];
  onFailure?: Activity[];
  
  metadata: ProcessMetadata;
}

export interface ProcessMetadata {
  author?: string;
  createdAt: string;
  updatedAt: string;
  lastExecutedAt?: string;
  executionCount: number;
  averageDuration?: number;
  successRate?: number;
}

export interface ProcessExecution {
  id: string;
  processId: string;
  processVersion: string;
  status: ExecutionStatus;
  triggeredBy: string; // Trigger ID or 'manual'
  
  startedAt: string;
  completedAt?: string;
  duration?: number;
  
  variables: Record<string, any>;
  activityResults: ActivityResult[];
  
  error?: ProcessError;
  logs: LogEntry[];
}

export type ExecutionStatus = 
  | 'pending'
  | 'running'
  | 'waiting' // Waiting for human input
  | 'paused'
  | 'completed'
  | 'failed'
  | 'cancelled';

export interface ActivityResult {
  activityId: string;
  status: 'success' | 'failed' | 'skipped';
  startedAt: string;
  completedAt: string;
  duration: number;
  outputs?: Record<string, any>;
  error?: string;
}

export interface ProcessError {
  activityId?: string;
  message: string;
  code?: string;
  stack?: string;
  retryable: boolean;
}

export interface LogEntry {
  timestamp: string;
  level: 'debug' | 'info' | 'warn' | 'error';
  activityId?: string;
  message: string;
  data?: any;
}

// Trigger Suggestion Types
export interface TriggerSuggestion {
  trigger: ProcessTrigger;
  confidence: number; // 0-1
  reasoning: string;
  alternatives: ProcessTrigger[];
  conflicts?: TriggerConflict[];
}

export interface TriggerConflict {
  processId: string;
  processName: string;
  triggerName: string;
  conflictType: 'time' | 'resource' | 'dependency';
  description: string;
  suggestion?: string;
}

export interface TriggerPattern {
  persona: PersonaType;
  activityTypes: string[];
  suggestedTriggers: TriggerTemplate[];
}

export interface TriggerTemplate {
  type: TriggerType;
  name: string;
  defaultConfig: Partial<TriggerConfig>;
  applicableWhen: string; // Description of when to use
}

// Process Templates
export interface ProcessTemplate {
  id: string;
  name: string;
  description: string;
  persona: PersonaType;
  category: string;
  
  variables: Array<{
    name: string;
    type: string;
    defaultValue?: any;
    required?: boolean;
    description?: string;
  }>;
  
  suggestedTriggers: TriggerTemplate[];
  activities: Activity[];
  
  estimatedDuration?: string;
  requiredIntegrations?: string[];
}