<?php
/**
 * ==========================
 * 🔐 IGNY8 FILE RULE HEADER
 * ==========================
 * @file         : model-rates-config.php
 * @location     : /ai/model-rates-config.php
 * @type         : Config Array
 * @scope        : Global
 * @allowed      : Model pricing, cost calculations, rate configurations
 * @reusability  : Globally Reusable
 * @notes        : Central AI model pricing configuration
 */

// Prevent direct access
if (!defined('ABSPATH')) {
    exit;
}

/**
 * Global model rates configuration
 * Rates are per 1 million tokens for text models
 * Per image for image generation models
 * GPT-4.1, GPT-4o-mini, and GPT-4o models are supported
 */
$IGNY8_MODEL_RATES = [
    'gpt-4.1' => ['in' => 2.00, 'out' => 8.00],
    'gpt-4o-mini' => ['in' => 0.15, 'out' => 0.60],
    'gpt-4o' => ['in' => 2.50, 'out' => 10.00]
];

/**
 * Global image model rates configuration
 * Rates are per image
 */
$IGNY8_IMAGE_MODEL_RATES = [
    'dall-e-3' => 0.040,
    'dall-e-2' => 0.020,
    'gpt-image-1' => 0.042,
    'gpt-image-1-mini' => 0.011
];

/**
 * Get model rates for a specific model
 * 
 * @param string $model Model name
 * @return array Model rates array with 'in' and 'out' keys
 */
function igny8_get_model_rates($model) {
    global $IGNY8_MODEL_RATES;
    return $IGNY8_MODEL_RATES[$model] ?? $IGNY8_MODEL_RATES['gpt-4.1'];
}

/**
 * Calculate API cost based on model and token usage
 * 
 * @param string $model Model name
 * @param int $input_tokens Number of input tokens
 * @param int $output_tokens Number of output tokens
 * @return array Cost breakdown with 'input_cost', 'output_cost', 'total_cost'
 */
function igny8_calculate_api_cost($model, $input_tokens, $output_tokens) {
    $rates = igny8_get_model_rates($model);
    
    // Debug logging
    error_log("Igny8 Cost Calc Debug: Model=$model, Rates=" . json_encode($rates));
    error_log("Igny8 Cost Calc Debug: Input tokens=$input_tokens, Output tokens=$output_tokens");
    
    $input_cost = ($input_tokens / 1000000) * $rates['in'];
    $output_cost = ($output_tokens / 1000000) * $rates['out'];
    $total_cost = $input_cost + $output_cost;
    
    error_log("Igny8 Cost Calc Debug: Input cost=$input_cost, Output cost=$output_cost, Total cost=$total_cost");
    
    return [
        'input_cost' => $input_cost,
        'output_cost' => $output_cost,
        'total_cost' => $total_cost,
        'model' => $model,
        'input_tokens' => $input_tokens,
        'output_tokens' => $output_tokens
    ];
}

/**
 * Format cost for display
 * 
 * @param float $cost Cost amount
 * @param int $decimals Number of decimal places
 * @return string Formatted cost string
 */
function igny8_format_cost($cost, $decimals = 4) {
    // Convert to cents for better readability
    $cents = $cost * 100;
    return number_format($cents, 2) . '¢';
}

/**
 * Get image model rates for a specific model
 * 
 * @param string $model Image model name
 * @return float Image model rate per image
 */
function igny8_get_image_model_rates($model) {
    global $IGNY8_IMAGE_MODEL_RATES;
    return $IGNY8_IMAGE_MODEL_RATES[$model] ?? $IGNY8_IMAGE_MODEL_RATES['dall-e-3'];
}

/**
 * Calculate image generation cost based on model
 * 
 * @param string $model Image model name
 * @param int $image_count Number of images generated
 * @return array Cost breakdown with 'per_image_cost', 'total_cost'
 */
function igny8_calculate_image_cost($model, $image_count = 1) {
    $per_image_rate = igny8_get_image_model_rates($model);
    $total_cost = $per_image_rate * $image_count;
    
    return [
        'per_image_cost' => $per_image_rate,
        'total_cost' => $total_cost,
        'model' => $model,
        'image_count' => $image_count
    ];
}

/**
 * Get image model display name with pricing and typical uses
 * 
 * @param string $model Image model name
 * @return string Formatted model name with pricing and uses
 */
function igny8_get_image_model_display_name($model) {
    $model_info = [
        'dall-e-3' => [
            'name' => 'DALL·E 3',
            'uses' => 'High-quality image generation with advanced AI capabilities'
        ],
        'dall-e-2' => [
            'name' => 'DALL·E 2',
            'uses' => 'Cost-effective image generation with good quality'
        ],
        'gpt-image-1' => [
            'name' => 'GPT Image 1 (Full)',
            'uses' => 'Full-featured image generation with comprehensive capabilities'
        ],
        'gpt-image-1-mini' => [
            'name' => 'GPT Image 1 Mini',
            'uses' => 'Lightweight, cost-effective image generation for bulk operations'
        ]
    ];
    
    $rate = igny8_get_image_model_rates($model);
    $info = $model_info[$model] ?? ['name' => strtoupper($model), 'uses' => 'Image generation'];
    
    return sprintf(
        '%s — $%.3f per image (%s)',
        $info['name'], $rate, $info['uses']
    );
}

/**
 * Get model display name with pricing and typical uses
 * 
 * @param string $model Model name
 * @return string Formatted model name with pricing and uses
 */
function igny8_get_model_display_name($model) {
    $model_info = [
        'gpt-4.1' => [
            'name' => 'GPT-4.1',
            'uses' => 'Content creation, coding, analysis, high-quality content generation'
        ],
        'gpt-4o-mini' => [
            'name' => 'GPT-4o mini',
            'uses' => 'Bulk tasks, lightweight AI, cost-effective for high-volume operations'
        ],
        'gpt-4o' => [
            'name' => 'GPT-4o',
            'uses' => 'Advanced AI for general and multimodal tasks, faster than GPT-4.1'
        ]
    ];
    
    $rates = igny8_get_model_rates($model);
    $info = $model_info[$model] ?? ['name' => strtoupper($model), 'uses' => 'General purpose'];
    
    return sprintf(
        '%s — $%.2f / $%.2f per 1M tokens (%s)',
        $info['name'], $rates['in'], $rates['out'], $info['uses']
    );
}
