import { Injectable } from '@nestjs/common';

export interface PricingFactors {
  faceValue: number;
  daysToExpiry: number;
  totalDays: number;
  redemptionRate: number;
  liquidityPremium: number;
}

@Injectable()
export class PricingService {
  /**
   * 3-factor pricing model: P = F × (1 - dt - rc - lp)
   * dt = time decay factor
   * rc = redemption credit (higher redemption rate = lower discount)
   * lp = liquidity premium
   */
  calculatePrice(factors: PricingFactors): { price: number; breakdown: Record<string, number> } {
    const { faceValue, daysToExpiry, totalDays, redemptionRate, liquidityPremium } = factors;
    const dt = totalDays > 0 ? Math.max(0, 1 - daysToExpiry / totalDays) * 0.3 : 0;
    const rc = (1 - redemptionRate) * 0.2;
    const lp = liquidityPremium;
    const discount = dt + rc + lp;
    const price = Math.max(faceValue * 0.1, faceValue * (1 - discount));
    return { price: Math.round(price * 100) / 100, breakdown: { faceValue, timeDecay: dt, redemptionCredit: rc, liquidityPremium: lp, totalDiscount: discount } };
  }
}
