# -*- coding: utf-8 -*-
"""
Dify BaaS API 客户端
取代原有的 OllamaClient，所有 AI 调用统一走 Dify 平台 API。

Dify 部署地址: http://192.168.1.88
文档参考: https://docs.dify.ai/guides/application-publishing/developing-with-apis
"""

import logging

import httpx

from app.core.config import settings

logger = logging.getLogger("dify_client")


class DifyClient:
    """
    Dify 平台 API 客户端（异步）。

    每个 Dify 应用有独立的 API Key:
    - 日志分析 App → DIFY_LOG_APP_API_KEY
    - 月度报告 App → DIFY_REPORT_APP_API_KEY
    调用时传入对应 key 即可。
    """

    def __init__(self, base_url: str = "http://192.168.1.88/v1"):
        self.base_url = base_url.rstrip("/")

    async def call_text_generator(
        self,
        api_key: str,
        inputs: dict,
        query: str = "",
    ) -> str:
        """
        调用 Dify 文本生成（completion）类应用。

        :param api_key: Dify App API Key (app-xxx 格式)
        :param inputs: 传入变量字典，键名需与 Dify 后台配置的变量名一致
        :param query: 可选的用户查询文本
        :return: Dify 返回的 answer 文本，失败时返回空字符串
        """
        url = f"{self.base_url}/completion-messages"
        headers = {
            "Authorization": f"Bearer {api_key}",
            "Content-Type": "application/json",
        }
        payload = {
            "inputs": inputs,
            "query": query,
            "response_mode": "blocking",
            "user": "crm-backend",
        }

        try:
            async with httpx.AsyncClient(timeout=60.0) as client:
                response = await client.post(url, headers=headers, json=payload)

            if response.status_code != 200:
                logger.error(
                    "Dify API 非 200 响应: status=%d body=%s",
                    response.status_code,
                    response.text[:500],
                )
                return ""

            data = response.json()
            answer = data.get("answer", "")
            logger.info(
                "Dify 调用成功: %d chars (key=...%s)",
                len(answer),
                api_key[-6:],
            )
            return answer

        except httpx.TimeoutException:
            logger.error("Dify API 超时 (60s): url=%s key=...%s", url, api_key[-6:])
            return ""

        except Exception as e:
            logger.error("Dify API 异常: %s (key=...%s)", e, api_key[-6:], exc_info=True)
            return ""

    async def call_workflow(
        self,
        api_key: str,
        inputs: dict,
        user: str = "crm-backend",
    ) -> dict | str:
        """
        调用 Dify 工作流（workflow）类应用。

        :param api_key: Dify App API Key (app-xxx 格式)
        :param inputs: 传入变量字典，键名需与 Dify 后台配置的变量名一致
        :param user: 用户标识
        :return: Dify 工作流返回的 outputs 字典，失败时返回空字符串
        """
        url = f"{self.base_url}/workflows/run"
        headers = {
            "Authorization": f"Bearer {api_key}",
            "Content-Type": "application/json",
        }
        payload = {
            "inputs": inputs,
            "response_mode": "blocking",
            "user": user,
        }

        try:
            async with httpx.AsyncClient(timeout=60.0) as client:
                response = await client.post(url, headers=headers, json=payload)

            if response.status_code != 200:
                logger.error(
                    "Dify Workflow 非 200 响应: status=%d body=%s",
                    response.status_code,
                    response.text[:500],
                )
                return ""

            data = response.json()
            outputs = data.get("data", {}).get("outputs", {})
            logger.info(
                "Dify Workflow 调用成功: outputs_keys=%s (key=...%s)",
                list(outputs.keys()) if isinstance(outputs, dict) else "N/A",
                api_key[-6:],
            )
            return outputs

        except httpx.TimeoutException:
            logger.error("Dify Workflow 超时 (60s): url=%s key=...%s", url, api_key[-6:])
            return ""

        except Exception as e:
            logger.error("Dify Workflow 异常: %s (key=...%s)", e, api_key[-6:], exc_info=True)
            return ""

# 全局单例，使用 settings 中配置的 Dify 地址
dify_client = DifyClient(base_url=settings.DIFY_BASE_URL)
