<?php

namespace XZD\AI\V5\AI\Providers;

use XZD\AI\V5\AI\OpenAICompatibleProvider;

/**
 * 小米 MiMo Provider
 *
 * Base URL: https://api.xiaomimimo.com/v1
 * 认证: api-key: {API_KEY}（也兼容 Authorization: Bearer）
 * 模型: mimo-v2-flash / mimo-v2-pro / mimo-v2-omni / mimo-v2.5 / mimo-v2.5-pro
 * 推理模式: extra_body.thinking.type = "enabled" | "disabled"
 *
 * @see https://mimo.mi.com/docs/zh-CN/quick-start/summary/welcome
 */
class MiMoProvider extends OpenAICompatibleProvider
{
    public static function defaultBaseUrl(): string
    {
        return 'https://api.xiaomimimo.com/v1';
    }

    public static function defaultModel(): string
    {
        return 'mimo-v2-flash';
    }

    public function getProviderKey(): string
    {
        return 'mimo';
    }

    /**
     * MiMo 使用 api-key 头认证（非 Authorization: Bearer）
     */
    protected function buildHeaders(): array
    {
        return [
            'api-key'      => $this->config['api_key'],
            'Content-Type' => 'application/json',
            'Accept'       => 'application/json',
        ];
    }

    /**
     * MiMo flash 模型不支持 thinking 参数，去掉避免 API 400
     */
    protected function buildRequestBody(array $messages, array $options, bool $stream): array
    {
        $body = parent::buildRequestBody($messages, $options, $stream);
        $model = $this->config['model'] ?: static::defaultModel();
        // flash 系列不支持推理模式，pro/omni 支持
        if (stripos($model, 'flash') !== false || stripos($model, 'tts') !== false) {
            unset($body['thinking']);
        }
        return $body;
    }
}
