"""Aliyun DashScope AI provider — OpenAI-compatible endpoint."""

from __future__ import annotations

import openai

from .base import AIProvider
from .base_provider import BaseAPIProvider


class AliyunProvider(BaseAPIProvider, AIProvider):
    """Aliyun DashScope provider using the OpenAI-compatible endpoint.

    Supports models: qwen-max, qwen-plus, qwen-turbo, qwen-long.
    API key obtained from: https://dashscope.console.aliyun.com/apiKey
    """

    name = "aliyun"
    BASE_URL = "https://dashscope.aliyuncs.com/compatible-mode/v1/"
    ENV_VAR = "DASHSCOPE_API_KEY"
    DEFAULT_MODEL = "qwen-max-latest"

    def _create_client(self) -> openai.OpenAI:
        http_client = self._make_http_client(timeout=90.0)
        kwargs = dict(
            api_key=self.api_key,
            base_url=self.BASE_URL,
            timeout=90.0,  # Extended for China: packet loss requires longer timeouts
        )
        if http_client:
            kwargs["http_client"] = http_client
        return openai.OpenAI(**kwargs)
