// Meta-Ledger System (MLS) - Type Definitions

export enum LedgerType {
  UTXO_CBDC = 'UTXO_CBDC', // UTXO-based CBDC chain
  ACCOUNT_CBDC = 'ACCOUNT_CBDC', // Account-based CBDC chain
  HYBRID_SOVEREIGN = 'HYBRID_SOVEREIGN', // Hybrid sovereign chain
  TOKENIZATION = 'TOKENIZATION', // Tokenization network
  DBIS_MASTER = 'DBIS_MASTER', // DBIS Master Ledger
  COMMODITY = 'COMMODITY', // Commodity token chain
  SECURITY = 'SECURITY', // Security token network
}

export interface LedgerState {
  ledgerId: string;
  ledgerType: LedgerType;
  sovereignBankId?: string;
  stateHash: string; // Hash of current state
  blockHeight: number;
  lastBlockHash: string;
  timestamp: Date;
  metadata?: Record<string, unknown>;
}

export interface MetaState {
  id: string;
  epoch: number;
  committedAt: Date;
  stateHash: string; // HASH(scb_states + cbdc_states + commodity_states + security_states)
  ledgerStates: LedgerState[];
  scbStates: LedgerState[]; // SCB ledger states
  cbdcStates: LedgerState[]; // CBDC chain states
  commodityStates: LedgerState[]; // Commodity token chain states
  securityStates: LedgerState[]; // Security token network states
  dbisMasterState: LedgerState; // DBIS Master Ledger state
}

export interface SynchronizationEvent {
  id: string;
  type: 'STATE_COMMIT' | 'CONFLICT_DETECTED' | 'RESOLUTION_APPLIED' | 'SYNC_COMPLETE';
  epoch: number;
  timestamp: Date;
  affectedLedgers: string[];
  details?: Record<string, unknown>;
}

export interface ConflictResolution {
  id: string;
  conflictType:
    | 'TIMESTAMP_CONFLICT'
    | 'DOUBLE_POST'
    | 'MULTI_CHAIN_RACE'
    | 'STATE_DIVERGENCE';
  affectedLedgers: string[];
  resolutionStrategy: 'SOVEREIGN_TRUST_WEIGHTING' | 'DETERMINISTIC_ORDERING' | 'MANUAL';
  resolvedState: Record<string, unknown>;
  resolvedAt: Date;
  resolvedBy?: string;
}

export interface SynchronizationConfig {
  settlementEpochSeconds: number; // How often to commit meta-state
  conflictResolutionStrategy: 'AUTO' | 'MANUAL' | 'HYBRID';
  sovereignTrustWeights: Record<string, number>; // Sovereign ID -> trust weight (0-1)
  deterministicOrderingRules: string[]; // Rules for deterministic ordering
}

export interface LedgerSyncRequest {
  ledgerId: string;
  ledgerType: LedgerType;
  stateHash: string;
  blockHeight: number;
  lastBlockHash: string;
  timestamp: Date;
}

export interface MetaStateCommitResult {
  metaState: MetaState;
  conflictsDetected: ConflictResolution[];
  synchronized: boolean;
}

