/**
 * Ticket Workflow Engine Types
 * TypeScript definitions for the refactored table-driven workflow engine
 */

import { TicketData, WorkflowSettings } from './workflow';

// ============================================================================
// Database Row Types
// ============================================================================

export interface TicketWorkflow {
  id: number;
  name: string;
  description: string | null;
  is_active: boolean;
  trigger_event: string;
  trigger_conditions: TriggerCondition[];
  sort_order: number;
  created_at: Date;
  updated_at: Date;
}

export interface TicketWorkflowStep {
  id: number;
  workflow_id: number;
  step_order: number;
  step_type: string;
  name: string;
  config: Record<string, any>;
  on_failure: 'continue' | 'stop' | 'skip_to';
  skip_to_step: number | null;
  is_active: boolean;
  condition: StepCondition | null;
  created_at: Date;
  updated_at: Date;
}

export interface TicketWorkflowExecution {
  id: number;
  workflow_id: number;
  ticket_id: number;
  ticket_number: string | null;
  status: 'pending' | 'running' | 'completed' | 'failed' | 'skipped';
  classification_method: 'robotic' | 'ai' | 'hybrid' | null;
  branch: 'service_desk' | 'noc' | 'soc' | null;
  context: Record<string, any>;
  field_changes: Record<string, { before: any; after: any }> | null;
  started_at: Date;
  completed_at: Date | null;
  duration_ms: number | null;
  error_message: string | null;
  created_at: Date;
}

export interface TicketWorkflowExecutionStep {
  id: number;
  execution_id: number;
  step_order: number;
  step_type: string;
  step_name: string | null;
  status: 'pending' | 'running' | 'completed' | 'failed' | 'skipped';
  input_data: Record<string, any> | null;
  output_data: Record<string, any> | null;
  started_at: Date | null;
  completed_at: Date | null;
  duration_ms: number | null;
  error_message: string | null;
}

// ============================================================================
// Workflow Execution Types
// ============================================================================

export interface TriggerCondition {
  field: string;
  operator: 'equals' | 'not_equals' | 'in' | 'not_in' | 'contains' | 'not_contains' | 'gt' | 'lt';
  value: any;
}

export interface StepCondition {
  field: string;
  operator: 'equals' | 'not_equals' | 'in' | 'not_in' | 'contains' | 'not_contains' | 'gt' | 'lt' | 'is_null' | 'is_not_null';
  value: any;
}

export interface WorkflowStepContext {
  // Original ticket data
  ticket: TicketData;

  // Workflow settings (from workflow_settings table)
  _settings: WorkflowSettings;

  // Accumulated classification results from classify steps
  classification?: Record<string, any>;

  // Validation result from validate step
  validation?: {
    is_valid: boolean;
    errors: Array<{ field: string; message: string; value: any }>;
  };

  // Field changes accumulated from all steps (will be written to Autotask)
  field_changes?: Record<string, { before: any; after: any }>;

  // Any other data accumulated by steps
  [key: string]: any;
}

export interface WorkflowStepResult {
  success: boolean;
  output?: Record<string, any>;
  error?: string;
}

export interface TicketWorkflowWithSteps extends TicketWorkflow {
  steps: TicketWorkflowStep[];
}

// ============================================================================
// Step Executor Function Type
// ============================================================================

export type WorkflowStepExecutorFn = (
  step: TicketWorkflowStep,
  context: WorkflowStepContext,
  executionId: number
) => Promise<WorkflowStepResult>;
