<?php

return [
    /*
     * OpenAI-compatible API configuration.
     *
     * Environment variables are optional, but recommended in deployment:
     * - LLM_API_BASE_URL
     * - LLM_API_KEY
     * - LLM_CHAT_MODEL
     * - LLM_EMBEDDING_MODEL
     * - BIGMODEL_API_BASE_URL
     * - BIGMODEL_API_KEY
     */
    'default' => [
        'base_url' => getenv('LLM_API_BASE_URL') ?: 'https://api.openai.com/v1',
        'api_key' => getenv('LLM_API_KEY') ?: '',
        'organization' => getenv('LLM_ORGANIZATION') ?: null,
        'project' => getenv('LLM_PROJECT') ?: null,
        'timeout' => (int) (getenv('LLM_API_TIMEOUT') ?: 60),
        'connect_timeout' => (int) (getenv('LLM_API_CONNECT_TIMEOUT') ?: 10),
    ],

    'chat' => [
        'model' => getenv('LLM_CHAT_MODEL') ?: 'gpt-4.1-mini',
        'temperature' => (float) (getenv('LLM_CHAT_TEMPERATURE') ?: 0.2),
        'max_tokens' => (int) (getenv('LLM_CHAT_MAX_TOKENS') ?: 1200),
        'stream' => false,
    ],

    'metadata' => [
        'enabled' => (getenv('LLM_METADATA_ENABLED') ?: 'true') !== 'false',
        'model' => getenv('LLM_METADATA_MODEL') ?: (getenv('LLM_CHAT_MODEL') ?: 'gpt-4.1-mini'),
        'max_input_chars' => (int) (getenv('LLM_METADATA_MAX_INPUT_CHARS') ?: 12000),
        'max_tokens' => (int) (getenv('LLM_METADATA_MAX_TOKENS') ?: 1200),
        'temperature' => (float) (getenv('LLM_METADATA_TEMPERATURE') ?: 0.1),
        'stream' => false,
        'response_format' => ['type' => 'json_object'],
        'thinking' => [
            'type' => getenv('LLM_METADATA_THINKING') ?: 'disabled',
        ],
        'retry' => [
            'enabled' => (getenv('LLM_METADATA_RETRY_ENABLED') ?: 'true') !== 'false',
            'max_attempts' => (int) (getenv('LLM_METADATA_RETRY_MAX_ATTEMPTS') ?: 3),
            'base_delay_ms' => (int) (getenv('LLM_METADATA_RETRY_BASE_DELAY_MS') ?: 1500),
            'max_delay_ms' => (int) (getenv('LLM_METADATA_RETRY_MAX_DELAY_MS') ?: 10000),
            'retry_statuses' => [429],
            'retry_error_codes' => ['1302', '1303', '1304', '1305', '1306', '1307', '1308'],
        ],
    ],

    'embedding' => [
        'base_url' => getenv('BIGMODEL_API_BASE_URL') ?: 'https://open.bigmodel.cn/api/paas/v4',
        'api_key' => getenv('BIGMODEL_API_KEY') ?: (getenv('LLM_EMBEDDING_API_KEY') ?: getenv('LLM_API_KEY')),
        'model' => getenv('LLM_EMBEDDING_MODEL') ?: 'embedding-3',
        'batch_size' => min(64, max(1, (int) (getenv('LLM_EMBEDDING_BATCH_SIZE') ?: 32))),
        'dimensions' => getenv('LLM_EMBEDDING_DIMENSIONS') !== false
            ? (int) getenv('LLM_EMBEDDING_DIMENSIONS')
            : 2048,
        'timeout' => (int) (getenv('LLM_EMBEDDING_TIMEOUT') ?: 60),
        'connect_timeout' => (int) (getenv('LLM_EMBEDDING_CONNECT_TIMEOUT') ?: 10),
        'retry' => [
            'enabled' => (getenv('LLM_EMBEDDING_RETRY_ENABLED') ?: 'true') !== 'false',
            'max_attempts' => (int) (getenv('LLM_EMBEDDING_RETRY_MAX_ATTEMPTS') ?: 3),
            'base_delay_ms' => (int) (getenv('LLM_EMBEDDING_RETRY_BASE_DELAY_MS') ?: 1500),
            'max_delay_ms' => (int) (getenv('LLM_EMBEDDING_RETRY_MAX_DELAY_MS') ?: 10000),
            'retry_statuses' => [429, 500, 502, 503, 504],
            'retry_error_codes' => ['1302', '1303', '1304', '1305', '1306', '1307', '1308'],
        ],
    ],

    'search_keywords' => [
        'enabled' => (getenv('LLM_SEARCH_KEYWORDS_ENABLED') ?: 'true') !== 'false',
        'model' => getenv('LLM_SEARCH_KEYWORDS_MODEL') ?: (getenv('LLM_METADATA_MODEL') ?: (getenv('LLM_CHAT_MODEL') ?: 'gpt-4.1-mini')),
        'temperature' => (float) (getenv('LLM_SEARCH_KEYWORDS_TEMPERATURE') ?: 0.1),
        'max_tokens' => (int) (getenv('LLM_SEARCH_KEYWORDS_MAX_TOKENS') ?: 1920),
        'timeout' => (int) (getenv('LLM_SEARCH_KEYWORDS_TIMEOUT') ?: 12),
        'connect_timeout' => (int) (getenv('LLM_SEARCH_KEYWORDS_CONNECT_TIMEOUT') ?: 5),
        'response_format' => ['type' => 'json_object'],
        'thinking' => [
            'type' => getenv('LLM_SEARCH_KEYWORDS_THINKING') ?: 'disabled',
        ],
        'retry' => [
            'enabled' => (getenv('LLM_SEARCH_KEYWORDS_RETRY_ENABLED') ?: 'true') !== 'false',
            'max_attempts' => (int) (getenv('LLM_SEARCH_KEYWORDS_RETRY_MAX_ATTEMPTS') ?: 3),
            'base_delay_ms' => (int) (getenv('LLM_SEARCH_KEYWORDS_RETRY_BASE_DELAY_MS') ?: 1500),
            'max_delay_ms' => (int) (getenv('LLM_SEARCH_KEYWORDS_RETRY_MAX_DELAY_MS') ?: 10000),
            'retry_statuses' => [429],
            'retry_error_codes' => ['1302', '1303', '1304', '1305', '1306', '1307', '1308'],
        ],
    ],
];
