<?php
if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

/**
 * AI Model Constants
 *
 * Single source of truth for all supported AI model identifiers and their
 * associated allowed options. Mirrors admin/assets/js/constants/modelOptionMap.js —
 * update BOTH files together whenever models change.
 *
 * @package Kitsune_AI_Platform
 */

// ---------------------------------------------------------------------------
// OpenAI
// ---------------------------------------------------------------------------

/** Standard GPT models (no reasoning effort parameter). */
define( 'KITSUNE_AI_PLATFORM_OPENAI_STANDARD_MODELS', array(
    'gpt-4o',
    'gpt-4o-mini',
    'gpt-4.1',
    'gpt-4.1-mini',
    'gpt-4.1-nano',
) );

/**
 * Default model used when no model is configured.
 * Must be the first entry in KITSUNE_AI_PLATFORM_OPENAI_STANDARD_MODELS.
 */
define( 'KITSUNE_AI_PLATFORM_DEFAULT_MODEL', KITSUNE_AI_PLATFORM_OPENAI_STANDARD_MODELS[0] );

/**
 * OpenAI reasoning models and their allowed reasoning_effort values.
 * null means the reasoning_effort option is not supported for that model.
 */
define( 'KITSUNE_AI_PLATFORM_OPENAI_REASONING_MODELS', array(
    'gpt-5.2'    => array( 'none', 'low', 'medium', 'high', 'xhigh' ),
    'gpt-5.1'    => array( 'none', 'low', 'medium', 'high' ),
    'gpt-5'      => array( 'minimal', 'low', 'medium', 'high' ),
    'gpt-5-mini' => array( 'minimal', 'low', 'medium', 'high' ),
    'gpt-5-nano' => null, // reasoning_effort not supported
    'o3'         => array( 'low', 'medium', 'high' ),
    'o3-mini'    => array( 'low', 'medium', 'high' ),
    'o4-mini'    => array( 'low', 'medium', 'high' ),
) );

// ---------------------------------------------------------------------------
// Anthropic
// ---------------------------------------------------------------------------

/** All supported Anthropic / Claude models. */
define( 'KITSUNE_AI_PLATFORM_ANTHROPIC_MODELS', array(
    'claude-opus-4-6-20260205',
    'claude-opus-4-5-20251101',
    'claude-sonnet-4-5-20250929',
    'claude-sonnet-4-20250514',
    'claude-haiku-4-5-20251001',
) );

/** Anthropic models that support extended thinking (thinking.budget_tokens). */
define( 'KITSUNE_AI_PLATFORM_ANTHROPIC_EXTENDED_THINKING_MODELS', array(
    'claude-opus-4-6-20260205',
    'claude-opus-4-5-20251101',
    'claude-sonnet-4-5-20250929',
    'claude-haiku-4-5-20251001',
) );

// ---------------------------------------------------------------------------
// Google / Gemini
// ---------------------------------------------------------------------------

/** Gemini 3 family (thinking_level option). */
define( 'KITSUNE_AI_PLATFORM_GOOGLE_GEMINI3_MODELS', array(
    'gemini-3-pro-preview',
    'gemini-3-flash-preview',
) );

/** Gemini 2.5 family (thinking_budget option). */
define( 'KITSUNE_AI_PLATFORM_GOOGLE_GEMINI25_MODELS', array(
    'gemini-2.5-pro',
    'gemini-2.5-flash',
    'gemini-2.5-flash-lite',
) );

/** All supported Google Gemini models (union of both families). */
define( 'KITSUNE_AI_PLATFORM_GOOGLE_MODELS', array_merge( KITSUNE_AI_PLATFORM_GOOGLE_GEMINI3_MODELS, KITSUNE_AI_PLATFORM_GOOGLE_GEMINI25_MODELS ) );

// ---------------------------------------------------------------------------
// Image Generation
// ---------------------------------------------------------------------------

/** OpenAI image generation models. */
define( 'KITSUNE_AI_PLATFORM_IMAGE_MODELS_OPENAI', array(
    'dall-e-3',
    'dall-e-2',
) );

/** Google image generation models. */
define( 'KITSUNE_AI_PLATFORM_IMAGE_MODELS_GOOGLE', array(
    'imagen-3.0-generate-001',
) );

/**
 * Per-model option definitions for image generation.
 *
 * Each entry maps a model ID to the list of extra parameters exposed in the UI.
 * Models with no API-level options use an empty array.
 * Used by:
 *   - kitsune_ai_platform_get_frontend_model_map() → window.kitsuneAiPlatformConfig.models.imageModels
 *   - modelOptionMap.js → getImageModelOptions()
 *   - class-kitsune-ai-platform-admin-forms-content.php → ajax_generate_image()
 */
define( 'KITSUNE_AI_PLATFORM_IMAGE_MODEL_OPTIONS', array(
    'dall-e-3' => array(
        array(
            'key'     => 'style',
            'label'   => 'Style',
            'type'    => 'enum',
            'values'  => array( 'vivid', 'natural' ),
            'default' => 'vivid',
        ),
        array(
            'key'     => 'quality',
            'label'   => 'Quality',
            'type'    => 'enum',
            'values'  => array( 'standard', 'hd' ),
            'default' => 'standard',
        ),
    ),
    'dall-e-2'                => array(), // no extra API options
    'imagen-3.0-generate-001' => array(), // no extra API options
) );

// ---------------------------------------------------------------------------
// Option definitions (mirrored from JS — now PHP is the source of truth)
// ---------------------------------------------------------------------------

/** Standard OpenAI model options (top_p, frequency_penalty, presence_penalty). */
define( 'KITSUNE_AI_PLATFORM_OPENAI_STANDARD_OPTIONS', array(
    array( 'key' => 'top_p',             'label' => 'Top P',             'type' => 'float', 'min' => 0,  'max' => 1,   'default' => 1 )
) );

/** Base Anthropic model options (top_p, top_k, max_tokens). */
define( 'KITSUNE_AI_PLATFORM_ANTHROPIC_BASE_OPTIONS', array(
    array( 'key' => 'top_p',      'label' => 'Top P',      'type' => 'float', 'min' => 0,   'max' => 1,      'default' => 1  ),
    array( 'key' => 'top_k',      'label' => 'Top K',      'type' => 'int',   'min' => 1,   'max' => 500,    'default' => 40 ),
    array( 'key' => 'max_tokens', 'label' => 'Max Tokens', 'type' => 'int',   'min' => 1,   'max' => 100000, 'default' => 1000 ),
) );

/** Anthropic extended thinking option. */
define( 'KITSUNE_AI_PLATFORM_ANTHROPIC_THINKING_OPTIONS', array(
    array( 'key' => 'thinking.budget_tokens', 'label' => 'Thinking Budget Tokens', 'type' => 'int', 'min' => 1024, 'max' => 128000, 'default' => 10000 ),
) );

/** Google Gemini 3 model options. */
define( 'KITSUNE_AI_PLATFORM_GOOGLE_GEMINI3_OPTIONS', array(
    array( 'key' => 'thinking_level', 'label' => 'Thinking Level', 'type' => 'enum', 'values' => array( 'minimal', 'low', 'medium', 'high' ), 'default' => 'medium' ),
    array( 'key' => 'top_p',          'label' => 'Top P',          'type' => 'float', 'min' => 0, 'max' => 1,  'default' => 0.95 )
) );

/** Google Gemini 2.5 model options. */
define( 'KITSUNE_AI_PLATFORM_GOOGLE_GEMINI25_OPTIONS', array(
    array( 'key' => 'top_p',             'label' => 'Top P',             'type' => 'float', 'min' => 0,  'max' => 1,      'default' => 0.95 )
) );

// ---------------------------------------------------------------------------
// Frontend model map helper
// ---------------------------------------------------------------------------

/**
 * Build the full model map array for use in wp_localize_script.
 *
 * The returned structure mirrors MODEL_OPTION_MAP in the old modelOptionMap.js
 * so that the JS helper functions remain unchanged.
 *
 * @return array
 */
function kitsune_ai_platform_get_frontend_model_map() {
    // Build reasoning model entries
    $reasoning = array();
    foreach ( KITSUNE_AI_PLATFORM_OPENAI_REASONING_MODELS as $model_id => $effort_values ) {
        $reasoning[ $model_id ] = array(
            'values'  => $effort_values, // null means effort param not supported
            'default' => is_array( $effort_values ) ? $effort_values[0] : null,
        );
    }

    return array(
        'defaultModel' => KITSUNE_AI_PLATFORM_DEFAULT_MODEL,
        'openai'       => array(
            'standard'  => array(
                'models'  => array_values( KITSUNE_AI_PLATFORM_OPENAI_STANDARD_MODELS ),
                'options' => KITSUNE_AI_PLATFORM_OPENAI_STANDARD_OPTIONS,
            ),
            'reasoning' => $reasoning,
        ),
        'anthropic'    => array(
            'models'           => array_values( KITSUNE_AI_PLATFORM_ANTHROPIC_MODELS ),
            'options'          => KITSUNE_AI_PLATFORM_ANTHROPIC_BASE_OPTIONS,
            'extendedThinking' => array(
                'supportedModels' => array_values( KITSUNE_AI_PLATFORM_ANTHROPIC_EXTENDED_THINKING_MODELS ),
                'options'         => KITSUNE_AI_PLATFORM_ANTHROPIC_THINKING_OPTIONS,
            ),
        ),
        'google'       => array(
            'gemini3'  => array(
                'models'  => array_values( KITSUNE_AI_PLATFORM_GOOGLE_GEMINI3_MODELS ),
                'options' => KITSUNE_AI_PLATFORM_GOOGLE_GEMINI3_OPTIONS,
            ),
            'gemini25' => array(
                'models'  => array_values( KITSUNE_AI_PLATFORM_GOOGLE_GEMINI25_MODELS ),
                'options' => KITSUNE_AI_PLATFORM_GOOGLE_GEMINI25_OPTIONS,
            ),
        ),
        // Image model options keyed by bare model ID
        'imageModels'  => KITSUNE_AI_PLATFORM_IMAGE_MODEL_OPTIONS,
    );
}

