// Sub-Ledger Service - FX, Securities, Commodities, Derivatives, CBDC, Custody, Collateral

import prisma from '@/shared/database/prisma';
import { Decimal } from '@prisma/client/runtime/library';
import { v4 as uuidv4 } from 'uuid';


/**
 * Sub-Ledger Service
 * Manages specialized sub-ledgers for different asset types
 */
export class SubLedgerService {
  /**
   * Create FX sub-ledger entry
   */
  async createFxSubLedgerEntry(
    ledgerEntryId: string,
    fxTradeId: string,
    baseCurrency: string,
    quoteCurrency: string,
    baseAmount: string,
    quoteAmount: string,
    fxRate: string
  ): Promise<void> {
    await prisma.fx_sub_ledger.create({
      data: {
        id: uuidv4(),
        ledgerEntryId,
        fxTradeId,
        baseCurrency,
        quoteCurrency,
        baseAmount: new Decimal(baseAmount),
        quoteAmount: new Decimal(quoteAmount),
        fxRate: new Decimal(fxRate),
        createdAt: new Date(),
        updatedAt: new Date(),
      },
    });
  }

  /**
   * Create Securities sub-ledger entry
   */
  async createSecuritiesSubLedgerEntry(
    ledgerEntryId: string,
    securityId: string,
    securityType: string,
    quantity: string,
    price?: string
  ): Promise<void> {
    await prisma.securities_sub_ledger.create({
      data: {
        id: uuidv4(),
        ledgerEntryId,
        securityId,
        securityType,
        quantity: new Decimal(quantity),
        price: price ? new Decimal(price) : null,
        createdAt: new Date(),
      },
    });
  }

  /**
   * Create Commodities sub-ledger entry
   */
  async createCommoditiesSubLedgerEntry(
    ledgerEntryId: string,
    commodityType: string,
    quantity: string,
    unit: string,
    price?: string
  ): Promise<void> {
    await prisma.commodities_sub_ledger.create({
      data: {
        id: uuidv4(),
        ledgerEntryId,
        commodityType,
        quantity: new Decimal(quantity),
        unit,
        price: price ? new Decimal(price) : null,
        createdAt: new Date(),
        updatedAt: new Date(),
      },
    });
  }

  /**
   * Create Derivatives sub-ledger entry
   */
  async createDerivativesSubLedgerEntry(
    ledgerEntryId: string,
    derivativeType: string,
    notionalAmount: string,
    markToMarket?: string
  ): Promise<void> {
    await prisma.derivatives_sub_ledger.create({
      data: {
        id: uuidv4(),
        ledgerEntryId,
        derivativeType,
        notionalAmount: new Decimal(notionalAmount),
        markToMarket: markToMarket ? new Decimal(markToMarket) : null,
        createdAt: new Date(),
        updatedAt: new Date(),
      },
    });
  }

  /**
   * Create CBDC sub-ledger entry
   */
  async createCbdcSubLedgerEntry(
    ledgerEntryId: string,
    cbdcIssuanceId: string,
    walletId: string | null,
    operationType: string,
    amount: string
  ): Promise<void> {
    await prisma.cbdc_sub_ledger.create({
      data: {
        id: uuidv4(),
        ledgerEntryId,
        cbdcIssuanceId,
        walletId,
        operationType,
        amount: new Decimal(amount),
        createdAt: new Date(),
        updatedAt: new Date(),
      },
    });
  }

  /**
   * Create Custody sub-ledger entry
   */
  async createCustodySubLedgerEntry(
    ledgerEntryId: string,
    custodianId: string,
    assetType: string,
    quantity: string
  ): Promise<void> {
    await prisma.custody_sub_ledger.create({
      data: {
        id: uuidv4(),
        ledgerEntryId,
        custodianId,
        assetType,
        quantity: new Decimal(quantity),
        createdAt: new Date(),
        updatedAt: new Date(),
      },
    });
  }

  /**
   * Create Collateral sub-ledger entry
   */
  async createCollateralSubLedgerEntry(
    ledgerEntryId: string,
    collateralType: string,
    pledgedAmount: string,
    valuation: string
  ): Promise<void> {
    await prisma.collateral_sub_ledger.create({
      data: {
        id: uuidv4(),
        ledgerEntryId,
        collateralType,
        pledgedAmount: new Decimal(pledgedAmount),
        valuation: new Decimal(valuation),
        createdAt: new Date(),
        updatedAt: new Date(),
      },
    });
  }

  /**
   * Get FX sub-ledger entries for a trade
   */
  async getFxSubLedgerEntries(fxTradeId: string) {
    return await prisma.fx_sub_ledger.findMany({
      where: { fxTradeId },
    });
  }

  /**
   * Get Securities sub-ledger entries for a security
   */
  async getSecuritiesSubLedgerEntries(securityId: string) {
    return await prisma.securities_sub_ledger.findMany({
      where: { securityId },
    });
  }

  /**
   * Get Commodities sub-ledger entries
   */
  async getCommoditiesSubLedgerEntries(commodityType: string) {
    return await prisma.commodities_sub_ledger.findMany({
      where: { commodityType },
    });
  }

  /**
   * Get CBDC sub-ledger entries for an issuance
   */
  async getCbdcSubLedgerEntries(cbdcIssuanceId: string) {
    return await prisma.cbdc_sub_ledger.findMany({
      where: { cbdcIssuanceId },
    });
  }
}

export const subLedgerService = new SubLedgerService();

