# Copyright (c) 2024-2026 xiefujin <490021684@qq.com>
# Licensed under GNU GPLv3, see LICENSE file for full license terms.

# 本地模拟模型
# utils/mock_model.py
"""
模拟本地模型，用于测试
"""

import asyncio
import json
import random
from typing import List, Dict, Any
from aacode.i18n import t


class MockModel:
    """模拟模型类"""

    def __init__(self):
        self.responses = {
            "default": [
                '我需要分析这个任务并制定计划。\n\nAction: run_shell\nAction Input: {"command": "ls"}',
                'Let me first check the project structure, then create the appropriate files.\n\nAction: run_shell\nAction Input: {"command": "cat > hello.py << \'EOF\'\\nprint(\'Hello, World!\')\\nEOF"}',
                "Task completed.\n\n",
                '我需要运行这个程序来验证。\n\nAction: run_shell\nAction Input: {"command": "python3 hello.py"}',
                "Program ran successfully, task completed.\n\n",
            ],
            "create_file": [
                '我将创建一个hello world程序。\n\nAction: run_shell\nAction Input: {"command": "cat > hello.py << \'EOF\'\\n#!/usr/bin/env python3\\nprint(\'Hello, World!\')\\nEOF"}'
            ],
            "run_file": [
                '让我运行这个程序。\n\nAction: run_shell\nAction Input: {"command": "python3 hello.py"}'
            ],
        }

    async def chat_completions_create(
        self,
        model: str,
        messages: List[Dict],
        temperature: float = 0.1,
        max_tokens: int = 2000,
    ):
        """模拟聊天完成"""

        # 获取最后一条用户消息
        last_message = ""
        for msg in reversed(messages):
            if msg.get("role") == "user":
                last_message = msg.get("content", "")
                break

        # 根据消息内容选择响应
        if "create" in last_message.lower():
            response_text = random.choice(self.responses["create_file"])
        elif "run" in last_message.lower():
            response_text = random.choice(self.responses["run_file"])
        else:
            response_text = random.choice(self.responses["default"])

        # 返回模拟响应
        class MockResponse:
            def __init__(self, content):
                self.choices = [MockChoice(content)]

        class MockChoice:
            def __init__(self, content):
                self.message = MockMessage(content)

        class MockMessage:
            def __init__(self, content):
                self.content = content

        return MockResponse(response_text)


async def create_mock_model_caller(model_config: Dict):
    """创建模拟模型调用器"""
    mock_model = MockModel()

    async def model_caller(messages: List[Dict]) -> str:
        try:
            # 如果有真实的API配置，尝试使用
            if model_config.get("api_key") and model_config.get("base_url"):
                try:
                    import openai

                    client = openai.AsyncOpenAI(
                        api_key=model_config.get("api_key"),
                        base_url=model_config.get("base_url"),
                    )

                    response = await client.chat.completions.create(
                        model=model_config.get("name", "gpt-4"),
                        messages=messages,  # type: ignore
                        temperature=model_config.get("temperature", 0.1),
                        max_tokens=model_config.get("max_tokens", 2000),
                    )

                    content = response.choices[0].message.content
                    return content if content is not None else ""
                except Exception as e:
                    print(f"🔄 Real model call failed, using mock model: {e}")

            # 使用模拟模型
            response = await mock_model.chat_completions_create(
                model=model_config.get("name", "mock"),
                messages=messages,
                temperature=model_config.get("temperature", 0.1),
                max_tokens=model_config.get("max_tokens", 2000),
            )

            return response.choices[0].message.content

        except Exception as e:
            return f"Model call failed: {str(e)}"

    return model_caller


if __name__ == "__main__":
    # 测试模拟模型
    async def test():
        caller = await create_mock_model_caller({})
        messages = [
            {"role": "system", "content": "你是一个AI助手"},
            {"role": "user", "content": "创建一个hello world程序"},
        ]
        response = await caller(messages)
        print(f"Response: {response}")

    asyncio.run(test())
