// SCB Admin Console - FI Management Service
// FI directory, Nostro/Vostro accounts

import prisma from '@/shared/database/prisma';
import { logger } from '@/infrastructure/monitoring/logger';

export interface FIInfo {
  fiId: string;
  fiName: string;
  bic?: string;
  lei?: string;
  regulatoryTier: string;
  apiEnabled: boolean;
  status: 'active' | 'suspended' | 'pending';
  maxDailyLimits: {
    byAssetType: Record<string, number>;
    byCorridor: Record<string, number>;
  };
}

export interface NostroVostroAccount {
  accountId: string;
  accountType: 'nostro' | 'vostro';
  counterpartyFI: string;
  currencyCode: string;
  balance: number;
  limits: {
    dailyLimit: number;
    perTransactionLimit: number;
  };
  status: 'active' | 'frozen' | 'closed';
}

export interface FIManagementDashboard {
  fiDirectory: FIInfo[];
  nostroVostroAccounts: NostroVostroAccount[];
}

export class FIManagementService {
  /**
   * Get FI management dashboard
   */
  async getFIManagementDashboard(scbId: string): Promise<FIManagementDashboard> {
    const [fiDirectory, nostroVostroAccounts] = await Promise.all([
      this.getFIDirectory(scbId),
      this.getNostroVostroAccounts(scbId),
    ]);

    return { fiDirectory, nostroVostroAccounts };
  }

  /**
   * Get FI directory
   */
  async getFIDirectory(scbId: string): Promise<FIInfo[]> {
    // Placeholder - would query actual FI table
    // For now, return empty array as FIs might be stored differently
    return [];
  }

  /**
   * Get Nostro/Vostro accounts
   */
  async getNostroVostroAccounts(scbId: string): Promise<NostroVostroAccount[]> {
    // Placeholder - would query Nostro/Vostro account table
    // These might be stored as BankAccount with specific types
    const accounts = await prisma.bankAccount.findMany({
      where: {
        sovereignBankId: scbId,
        accountType: {
          in: ['nostro', 'vostro'],
        },
      },
      take: 100,
    });

    return accounts.map((account) => ({
      accountId: account.accountId,
      accountType: account.accountType as 'nostro' | 'vostro',
      counterpartyFI: account.counterpartyId || 'Unknown',
      currencyCode: account.currencyCode,
      balance: parseFloat(account.balance.toString()),
      limits: {
        dailyLimit: 1000000, // Placeholder
        perTransactionLimit: 100000, // Placeholder
      },
      status: account.status === 'active' ? 'active' : 'frozen',
    }));
  }
}

export const fiManagementService = new FIManagementService();

