"""DeepSeek LLM provider (OpenAI-compatible API)."""

import os

from alive.llm.base import LLMProvider


class DeepSeekProvider(LLMProvider):
    name = "deepseek"

    def __init__(self) -> None:
        import openai

        api_key = os.environ.get("ALIVE_DEEPSEEK_KEY") or os.environ.get("DEEPSEEK_API_KEY")
        if not api_key:
            raise ValueError(
                "No DeepSeek API key found.\n\n"
                "Fix: Set your API key with one of:\n"
                "  alive config --deepseek-key sk-...\n"
                "  export DEEPSEEK_API_KEY=sk-...\n\n"
                "Or switch to a local model: alive config --provider ollama"
            )
        self.client = openai.AsyncOpenAI(
            api_key=api_key,
            base_url="https://api.deepseek.com",
        )
        self.model = os.environ.get("ALIVE_DEEPSEEK_MODEL", "deepseek-chat")

    async def complete(self, system_prompt: str, user_prompt: str) -> str:
        response = await self.client.chat.completions.create(
            model=self.model,
            messages=[
                {"role": "system", "content": system_prompt},
                {"role": "user", "content": user_prompt},
            ],
        )
        return response.choices[0].message.content or ""
