// Sovereign AI Risk Engine (SARE) - Type Definitions

export enum RiskSubsystem {
  LSA = 'LSA', // Liquidity Shock Analyzer
  FXCP = 'FXCP', // FX Collapse Predictor
  CNHE = 'CNHE', // CBDC Network Health Engine
  CSM = 'CSM', // Commodity Stress Model
}

export interface RiskDataInputs {
  // Daily sovereign liquidity reports
  sovereignLiquidityReports?: {
    sovereignBankId: string;
    availableLiquidity: number;
    requiredLiquidity: number;
    timestamp: Date;
  }[];

  // Cross-border flow graphs
  crossBorderFlows?: {
    fromSovereign: string;
    toSovereign: string;
    volume: number;
    timestamp: Date;
  }[];

  // FX volatility surfaces
  fxVolatility?: {
    currencyPair: string;
    volatility: number;
    timestamp: Date;
  }[];

  // CBDC behavior metrics
  cbdcMetrics?: {
    sovereignBankId: string;
    transactionVolume: number;
    walletCount: number;
    velocity: number;
    timestamp: Date;
  }[];

  // Commodity reserve readings
  commodityReserves?: {
    commodityType: string;
    reserveAmount: number;
    backingRatio: number;
    timestamp: Date;
  }[];

  // Trade finance exposure
  tradeFinanceExposure?: {
    sovereignBankId: string;
    exposure: number;
    riskLevel: 'LOW' | 'MEDIUM' | 'HIGH';
    timestamp: Date;
  }[];

  // Smart contract execution patterns
  smartContractPatterns?: {
    contractId: string;
    executionCount: number;
    failureRate: number;
    timestamp: Date;
  }[];
}

export interface RiskOutputs {
  // Sovereign Stress Score (SSS) - 0-100 scale
  sovereignStressScore: {
    sovereignBankId: string;
    score: number; // 0-100, predictive of sovereign failure
    timestamp: Date;
    factors: {
      liquidityRatio: number;
      fxStability: number;
      cbdcHealth: number;
      commodityBacking: number;
    };
  }[];

  // FX Shock Probability (FXSP)
  fxShockProbability: {
    currencyPair: string;
    probability: number; // 0-1, probability of 10%+ volatility movement
    timestamp: Date;
    confidence: number; // 0-1
  }[];

  // CBDC Liquidity Tension Index (CLTI)
  cbdcLiquidityTension: {
    sovereignBankId: string;
    index: number; // Signals liquidity strain
    timestamp: Date;
    severity: 'LOW' | 'MEDIUM' | 'HIGH' | 'CRITICAL';
  }[];

  // Commodity Depletion Alert (CDA)
  commodityDepletionAlert: {
    commodityType: string;
    depletionRate: number;
    estimatedDaysUntilCritical: number;
    timestamp: Date;
    alertLevel: 'WARNING' | 'CRITICAL' | 'EMERGENCY';
  }[];
}

export interface SystemAction {
  id: string;
  type:
    | 'LIQUIDITY_TOPUP'
    | 'FX_BAND_TIGHTENING'
    | 'CBDC_THROTTLING'
    | 'SCB_RISK_ESCALATION'
    | 'COMMODITY_INTERVENTION';
  target: string; // sovereignBankId, currencyPair, etc.
  parameters: Record<string, unknown>;
  triggeredAt: Date;
  executed: boolean;
  executedAt?: Date;
}

export interface LSAOutput {
  sovereignBankId: string;
  liquidityShockProbability: number; // 0-1
  estimatedShockMagnitude: number;
  recommendedAction: string;
  timestamp: Date;
}

export interface FXCPOutput {
  currencyPair: string;
  collapseProbability: number; // 0-1
  expectedVolatility: number;
  confidence: number;
  timestamp: Date;
}

export interface CNHEOutput {
  sovereignBankId: string;
  networkHealthScore: number; // 0-100
  issues: string[];
  recommendations: string[];
  timestamp: Date;
}

export interface CSMOutput {
  commodityType: string;
  stressLevel: number; // 0-100
  depletionRate: number;
  criticalThreshold: number;
  timestamp: Date;
}

