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

export interface OverageResult {
  overageTokens: number;
  overageMTokens: number;
  overageCentsUsd: number;
}

@Injectable()
export class OverageCalculatorService {
  /**
   * Calculate overage charges for tokens consumed beyond the included quota.
   * @param totalTokens - total tokens consumed in the period
   * @param includedTokens - tokens included in the plan (no extra charge)
   * @param overageRateCentsPerMToken - price per 1M overage tokens in USD cents
   */
  calculate(totalTokens: number, includedTokens: number, overageRateCentsPerMToken: number): OverageResult {
    const overageTokens = Math.max(0, totalTokens - includedTokens);
    const overageMTokens = overageTokens / 1_000_000;
    const overageCentsUsd = Math.ceil(overageMTokens * overageRateCentsPerMToken);
    return { overageTokens, overageMTokens, overageCentsUsd };
  }
}
