<?php

declare(strict_types=1);

namespace ImovelZapAi\AiGateway\Application\Services;

use ImovelZapAi\AiGateway\Application\Data\AiTokenUsageData;

final class AiCostCalculator
{
    /**
     * @param  array<string, array{input: float, output: float}>  $pricing
     */
    public function __construct(
        private readonly array $pricing,
    ) {}

    public function estimate(string $model, AiTokenUsageData $usage): float
    {
        $rates = $this->pricing[$model] ?? $this->pricing['default'] ?? null;

        if ($rates === null) {
            return 0.0;
        }

        $inputCost = ($usage->inputTokens / 1_000_000) * (float) $rates['input'];
        $outputCost = ($usage->outputTokens / 1_000_000) * (float) $rates['output'];

        return round($inputCost + $outputCost, 8);
    }
}
