<?php
/**
 * AI Magic Cost Calculator
 * 
 * Calculates API costs based on model, input tokens, and output tokens
 *
 * @package AI_Magic
 */

if (!defined('ABSPATH')) {
    exit;
}

class AI_Magic_Cost_Calculator
{

    /**
     * Model pricing per 1K tokens (input / output)
     * Prices in USD
     */
    private static $pricing = [
        // O1 Series
        'o1-preview' => ['input' => 0.015, 'output' => 0.060],
        'o1-mini' => ['input' => 0.003, 'output' => 0.012],

        // GPT-4o Series
        'gpt-4o' => ['input' => 0.0025, 'output' => 0.010],
        'gpt-4o-2024-11-20' => ['input' => 0.0025, 'output' => 0.010],
        'gpt-4o-2024-08-06' => ['input' => 0.0025, 'output' => 0.010],
        'gpt-4o-2024-05-13' => ['input' => 0.005, 'output' => 0.015],
        'gpt-4o-mini' => ['input' => 0.00015, 'output' => 0.0006],
        'gpt-4o-mini-2024-07-18' => ['input' => 0.00015, 'output' => 0.0006],

        // GPT-4 Turbo
        'gpt-4-turbo' => ['input' => 0.01, 'output' => 0.03],
        'gpt-4-turbo-2024-04-09' => ['input' => 0.01, 'output' => 0.03],
        'gpt-4-turbo-preview' => ['input' => 0.01, 'output' => 0.03],
        'gpt-4-0125-preview' => ['input' => 0.01, 'output' => 0.03],
        'gpt-4-1106-preview' => ['input' => 0.01, 'output' => 0.03],

        // GPT-4
        'gpt-4' => ['input' => 0.03, 'output' => 0.06],
        'gpt-4-0613' => ['input' => 0.03, 'output' => 0.06],
        'gpt-4-32k' => ['input' => 0.06, 'output' => 0.12],
        'gpt-4-32k-0613' => ['input' => 0.06, 'output' => 0.12],

        // GPT-3.5 Turbo
        'gpt-3.5-turbo' => ['input' => 0.0005, 'output' => 0.0015],
        'gpt-3.5-turbo-0125' => ['input' => 0.0005, 'output' => 0.0015],
        'gpt-3.5-turbo-1106' => ['input' => 0.001, 'output' => 0.002],
        'gpt-3.5-turbo-16k' => ['input' => 0.003, 'output' => 0.004],
        'gpt-3.5-turbo-instruct' => ['input' => 0.0015, 'output' => 0.002],
    ];

    /**
     * Calculate cost for API call
     *
     * @param string $model Model name
     * @param int $input_tokens Number of input tokens
     * @param int $output_tokens Number of output tokens
     * @return float Total cost in USD
     */
    public static function calculate_cost($model, $input_tokens, $output_tokens)
    {
        $pricing = self::get_model_pricing($model);

        if (!$pricing) {
            return 0.0;
        }

        $input_cost = ($input_tokens / 1000) * $pricing['input'];
        $output_cost = ($output_tokens / 1000) * $pricing['output'];

        return round($input_cost + $output_cost, 6);
    }

    /**
     * Get pricing for a specific model
     *
     * @param string $model Model name
     * @return array|null Pricing array or null if not found
     */
    public static function get_model_pricing($model)
    {
        return isset(self::$pricing[$model]) ? self::$pricing[$model] : null;
    }

    /**
     * Estimate token count from text
     * Rough estimation: 1 token ≈ 4 characters
     *
     * @param string $text Text to estimate
     * @return int Estimated token count
     */
    public static function estimate_tokens($text)
    {
        return (int) ceil(strlen($text) / 4);
    }

    /**
     * Format cost as currency
     *
     * @param float $cost Cost in USD
     * @return string Formatted cost
     */
    public static function format_cost($cost)
    {
        if ($cost < 0.01) {
            return '$' . number_format($cost, 6);
        }
        return '$' . number_format($cost, 4);
    }

    /**
     * Get all available models with pricing
     *
     * @return array Models with pricing
     */
    public static function get_all_pricing()
    {
        return self::$pricing;
    }

    /**
     * Check if model has pricing data
     *
     * @param string $model Model name
     * @return bool True if pricing exists
     */
    public static function has_pricing($model)
    {
        return isset(self::$pricing[$model]);
    }
}
