// Securities Engine

import prisma from '@/shared/database/prisma';
import { Decimal } from '@prisma/client/runtime/library';
import { subLedgerService } from '@/core/ledger/subledgers/subledger.service';
import { v4 as uuidv4 } from 'uuid';


export class SecuritiesService {
  /**
   * Register a security
   */
  async registerSecurity(
    securityId: string,
    securityType: string,
    issuer: string,
    currencyCode: string,
    quantity: string,
    price?: string
  ): Promise<void> {
    await prisma.security.create({
      data: {
        securityId,
        securityType,
        issuer,
        currencyCode,
        quantity: new Decimal(quantity),
        price: price ? new Decimal(price) : null,
        status: 'active',
      },
    });
  }

  /**
   * Transfer security
   */
  async transferSecurity(
    securityId: string,
    fromAccountId: string,
    toAccountId: string,
    quantity: string,
    ledgerEntryId: string
  ): Promise<void> {
    const security = await prisma.security.findUnique({
      where: { securityId },
    });

    if (!security) {
      throw new Error('Security not found');
    }

    // Create securities sub-ledger entry
    await subLedgerService.createSecuritiesSubLedgerEntry(
      ledgerEntryId,
      securityId,
      security.securityType,
      quantity,
      security.price?.toString()
    );
  }
}

export const securitiesService = new SecuritiesService();

