/**
 * Order-to-Cash Autonomous System Types
 */

export type OrderToCashStage = 
  | 'order_placed'
  | 'payment_processing'
  | 'payment_confirmed'
  | 'invoice_generated'
  | 'transaction_recorded'
  | 'ar_updated'
  | 'fulfilled'
  | 'cash_reconciled'
  | 'completed';

export type AgentActionType =
  | 'create_checkout_session'
  | 'process_payment'
  | 'confirm_payment'
  | 'generate_invoice'
  | 'create_transaction'
  | 'update_ar'
  | 'send_confirmation'
  | 'update_inventory'
  | 'detect_anomaly'
  | 'create_sniff_check'
  | 'auto_approve'
  | 'record_pattern'
  | 'propose_modification'
  | 'dunning_escalation'
  | 'bank_reconciliation'
  | 'discrepancy_resolution'
  | 'inventory_alert'
  | 'ap_process'
  | 'expense_categorize'
  | 'dark_factory_job';

export type AnomalyType =
  | 'high_value_order'
  | 'unusual_quantity'
  | 'suspicious_email'
  | 'tax_mismatch'
  | 'duplicate_order'
  | 'rapid_orders'
  | 'shipping_mismatch'
  | 'payment_failure_pattern';

export type SniffCheckPriority = 'low' | 'normal' | 'high' | 'critical';

export type SniffCheckStatus = 'pending' | 'approved' | 'rejected' | 'auto_approved' | 'expired';

export interface AgentContext {
  executionId: string;
  orderId: string;
  orderNumber: string;
  agentName: string;
  timestamp: Date;
}

export interface AnomalyResult {
  detected: boolean;
  type?: AnomalyType;
  severity: SniffCheckPriority;
  details?: Record<string, unknown>;
  confidence: number;
}

export interface StageResult {
  success: boolean;
  stage: OrderToCashStage;
  nextStage?: OrderToCashStage;
  data?: Record<string, unknown>;
  anomaly?: AnomalyResult;
  requiresHumanReview?: boolean;
  error?: string;
}

export interface PatternData {
  patternType: string;
  patternKey: string;
  value: unknown;
  sampleSize: number;
  confidence: number;
}

export interface SelfModificationProposal {
  proposalType: 'config_change' | 'new_rule' | 'workflow_update' | 'tool_creation';
  title: string;
  description: string;
  rationale: string;
  targetSystem: string;
  currentState: Record<string, unknown>;
  proposedState: Record<string, unknown>;
  riskLevel: 'low' | 'medium' | 'high' | 'critical';
  confidence: number;
}

// Threshold configurations (can be self-modified)
export const DEFAULT_THRESHOLDS = {
  highValueOrderAmount: 500, // Orders above this trigger review
  unusualQuantityThreshold: 10, // Single item quantity above this triggers review
  rapidOrderWindowMinutes: 5, // Time window to check for rapid orders
  rapidOrderCount: 3, // Number of orders in window to trigger review
  taxMismatchTolerance: 0.02, // 2% tolerance for tax calculations
  patternConfidenceThreshold: 0.8, // Minimum confidence to apply learned pattern
  autoApproveDelayMinutes: 30, // Auto-approve sniff-checks after this time if low risk
};
