// DBIS Nostro/Vostro Network - Type Definitions

import { Decimal } from '@prisma/client/runtime/library';

// ============================================================================
// Participant Types
// ============================================================================

export enum RegulatoryTier {
  SCB = 'SCB', // Sovereign Central Bank
  TIER1 = 'Tier1', // Tier 1 Financial Institution
  TIER2 = 'Tier2', // Tier 2 Financial Institution
  PSP = 'PSP', // Payment Service Provider
}

export interface Participant {
  participantId: string;
  name: string;
  bic?: string;
  lei?: string;
  country: string; // ISO 3166-1 alpha-2
  regulatoryTier: RegulatoryTier;
  sovereignBankId?: string;
  status: 'active' | 'suspended' | 'inactive';
  metadata?: Record<string, unknown>;
  createdAt: Date;
  updatedAt: Date;
}

export interface ParticipantCreateRequest {
  participantId?: string; // Auto-generated if not provided
  name: string;
  bic?: string;
  lei?: string;
  country: string;
  regulatoryTier: RegulatoryTier;
  sovereignBankId?: string;
  metadata?: Record<string, unknown>;
}

// ============================================================================
// Account Types
// ============================================================================

export enum AccountType {
  NOSTRO = 'NOSTRO', // "Our money with you"
  VOSTRO = 'VOSTRO', // "Your money with us"
}

export enum AccountStatus {
  ACTIVE = 'ACTIVE',
  SUSPENDED = 'SUSPENDED',
  CLOSED = 'CLOSED',
}

export interface NostroVostroAccount {
  accountId: string;
  ownerParticipantId: string;
  counterpartyParticipantId: string;
  ibanOrLocalAccount?: string;
  currency: string; // ISO 4217
  accountType: AccountType;
  status: AccountStatus;
  currentBalance: string; // Decimal as string
  availableLiquidity: string; // Decimal as string
  holdAmount: string; // Decimal as string
  lastUpdatedAt: Date;
  metadata?: Record<string, unknown>;
  createdAt: Date;
  updatedAt: Date;
}

export interface AccountCreateRequest {
  ownerParticipantId: string;
  counterpartyParticipantId: string;
  ibanOrLocalAccount?: string;
  currency: string;
  accountType: AccountType;
  metadata?: Record<string, unknown>;
}

export interface BalanceResponse {
  accountId: string;
  currentBalance: string;
  availableLiquidity: string;
  holdAmount: string;
  currency: string;
  lastUpdatedAt: Date;
}

// ============================================================================
// Transfer Types
// ============================================================================

export enum SettlementAsset {
  FIAT = 'FIAT',
  GRU = 'GRU',
  SSU = 'SSU',
  CBDC = 'CBDC',
}

export enum TransferStatus {
  PENDING = 'PENDING',
  ACCEPTED = 'ACCEPTED',
  SETTLED = 'SETTLED',
  REJECTED = 'REJECTED',
  CANCELLED = 'CANCELLED',
}

export interface FxDetails {
  rate?: string;
  fromCurrency: string;
  toCurrency: string;
  convertedAmount?: string;
  fxProvider?: string;
}

export interface NostroVostroTransfer {
  transferId: string;
  fromAccountId: string;
  toAccountId: string;
  fromParticipantId: string;
  toParticipantId: string;
  amount: string; // Decimal as string
  currency: string;
  settlementAsset: SettlementAsset;
  valueDate: Date;
  fxDetails?: FxDetails;
  status: TransferStatus;
  rejectionReason?: string;
  idempotencyKey?: string;
  reference?: string;
  metadata?: Record<string, unknown>;
  settledAt?: Date;
  createdAt: Date;
  updatedAt: Date;
}

export interface TransferCreateRequest {
  fromAccountId: string;
  toAccountId: string;
  amount: string;
  currency: string;
  settlementAsset?: SettlementAsset;
  valueDate?: string; // ISO 8601 date string
  fxDetails?: FxDetails;
  reference?: string;
  metadata?: Record<string, unknown>;
}

// ============================================================================
// Reconciliation Types
// ============================================================================

export interface ReconciliationBreak {
  breakType: string;
  description: string;
  amount?: string;
  transactionId?: string;
  details?: Record<string, unknown>;
}

export interface NostroVostroReconciliation {
  reportId: string;
  participantId: string;
  asOfDate: Date;
  openingBalance: string;
  closingBalance: string;
  totalDebits: string;
  totalCredits: string;
  breakCount: number;
  status: 'PENDING' | 'COMPLETED' | 'FAILED';
  breaks?: ReconciliationBreak[];
  metadata?: Record<string, unknown>;
  completedAt?: Date;
  createdAt: Date;
  updatedAt: Date;
}

export interface ReconciliationRequest {
  participantId: string;
  asOfDate: string; // ISO 8601 date string
  accountId?: string; // Optional: specific account reconciliation
  metadata?: Record<string, unknown>;
}

// ============================================================================
// Webhook Types
// ============================================================================

export enum WebhookEventType {
  TRANSFER_CREATED = 'TRANSFER_CREATED',
  TRANSFER_SETTLED = 'TRANSFER_SETTLED',
  TRANSFER_REJECTED = 'TRANSFER_REJECTED',
  ACCOUNT_UPDATED = 'ACCOUNT_UPDATED',
  BALANCE_CHANGED = 'BALANCE_CHANGED',
  RECONCILIATION_COMPLETED = 'RECONCILIATION_COMPLETED',
}

export interface WebhookSubscription {
  subscriptionId: string;
  participantId: string;
  webhookUrl: string;
  eventTypes: WebhookEventType[];
  status: 'ACTIVE' | 'SUSPENDED' | 'INACTIVE';
  lastDeliveryAt?: Date;
  failureCount: number;
  metadata?: Record<string, unknown>;
  createdAt: Date;
  updatedAt: Date;
}

export interface WebhookSubscriptionCreateRequest {
  participantId: string;
  webhookUrl: string;
  eventTypes: WebhookEventType[];
  metadata?: Record<string, unknown>;
}

export interface WebhookEvent {
  eventId: string;
  subscriptionId: string;
  eventType: WebhookEventType;
  payload: Record<string, unknown>;
  status: 'PENDING' | 'DELIVERED' | 'FAILED' | 'RETRYING';
  deliveryAttempts: number;
  lastAttemptAt?: Date;
  deliveredAt?: Date;
  errorMessage?: string;
  createdAt: Date;
  updatedAt: Date;
}

// ============================================================================
// GRU/FX Extension Types
// ============================================================================

export interface GruFxRate {
  pair: string; // e.g., "USD/GRU", "EUR/GRU"
  rate: string;
  timestamp: Date;
  source: string; // Rate source/provider
  bid?: string;
  ask?: string;
  spread?: string;
}

export interface GruFxRateHistory {
  pair: string;
  rates: Array<{
    rate: string;
    timestamp: Date;
  }>;
  startDate: Date;
  endDate: Date;
}

export interface GruFxConversionRequest {
  fromCurrency: string;
  toCurrency: string;
  amount: string;
  settlementAsset?: SettlementAsset;
  valueDate?: string;
}

export interface GruFxConversionResponse {
  conversionId: string;
  fromCurrency: string;
  toCurrency: string;
  fromAmount: string;
  toAmount: string;
  rate: string;
  settlementAsset: SettlementAsset;
  timestamp: Date;
}

// ============================================================================
// API Response Types
// ============================================================================

export interface PaginatedResponse<T> {
  data: T[];
  pagination: {
    page: number;
    pageSize: number;
    total: number;
    totalPages: number;
  };
}

export interface ErrorResponse {
  error: {
    code: string;
    message: string;
    details?: Record<string, unknown>;
  };
  timestamp: Date;
}

