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

interface PricingTier {
  inputPricePerM: number;  // ¥ per million input tokens
  outputPricePerM: number; // ¥ per million output tokens
}

@Injectable()
export class AiCostCalculatorService {
  private readonly pricing: Record<string, PricingTier> = {
    'deepseek-v4-flash': { inputPricePerM: 1, outputPricePerM: 2 },
    'deepseek-v4-pro': { inputPricePerM: 3, outputPricePerM: 6 },
  };

  calculate(provider: string, model: string, inputTokens: number, outputTokens: number): number {
    if (provider === 'mock') return 0;

    const tier = this.pricing[model];
    if (!tier) return 0;

    const inputCost = (inputTokens / 1_000_000) * tier.inputPricePerM;
    const outputCost = (outputTokens / 1_000_000) * tier.outputPricePerM;

    return Math.round((inputCost + outputCost) * 10000) / 10000;
  }
}
