import logging
from typing import List

from fastapi import APIRouter, Query, Body, HTTPException

from app.controllers.third_party_api import third_party_api_controller
from app.schemas.base import Success, Fail
from app.schemas.third_party_api import (
    BaseAPIRequest,
    ChinazAPIRequest,
    OCRRequest,
    XiaohongshuNoteRequest,
    JizhiliaoSearchRequest,
    DifyWorkflowRequest,
    APIResponse,
    APIProviderInfo,
    APIEndpointInfo,
    APIListResponse
)

logger = logging.getLogger(__name__)

router = APIRouter(tags=["内置接口"])

# 站长之家API端点

@router.post("/chinaz/copyright_software", summary="企业软件著作权查询")
async def query_copyright_software(request: ChinazAPIRequest):
    """查询企业软件著作权信息"""
    try:
        result = await third_party_api_controller.query_copyright_software(
            company_name=request.company_name,
            chinaz_ver=request.chinaz_ver
        )
        
        if result.success:
            return Success(data=result.data, message=result.message)
        else:
            return Fail(message=result.message)
    except Exception as e:
        logger.error(f"查询企业软件著作权失败: {e}")
        return Fail(message=f"查询企业软件著作权失败: {str(e)}")

@router.post("/chinaz/patent", summary="企业专利信息查询")
async def query_patent_info(request: ChinazAPIRequest):
    """查询企业专利信息"""
    try:
        result = await third_party_api_controller.query_patent_info(
            company_name=request.company_name,
            chinaz_ver=request.chinaz_ver
        )
        
        if result.success:
            return Success(data=result.data, message=result.message)
        else:
            return Fail(message=result.message)
    except Exception as e:
        logger.error(f"查询企业专利信息失败: {e}")
        return Fail(message=f"查询企业专利信息失败: {str(e)}")

@router.post("/chinaz/ocr", summary="OCR图片识别")
async def recognize_ocr(request: OCRRequest):
    """OCR图片识别接口"""
    try:
        result = await third_party_api_controller.recognize_ocr(
            image_url=request.url,
            chinaz_ver=request.chinaz_ver
        )
        
        if result.success:
            return Success(data=result.data, message=result.message)
        else:
            return Fail(message=result.message)
    except Exception as e:
        logger.error(f"OCR识别失败: {e}")
        return Fail(message=f"OCR识别失败: {str(e)}")

# 小红书API端点

@router.post("/xiaohongshu/note", summary="获取小红书笔记详情")
async def get_xiaohongshu_note(request: XiaohongshuNoteRequest):
    """获取小红书笔记详情"""
    try:
        result = await third_party_api_controller.get_xiaohongshu_note(
            note_id=request.note_id
        )
        
        if result.success:
            return Success(data=result.data, message=result.message)
        else:
            return Fail(message=result.message)
    except Exception as e:
        logger.error(f"获取小红书笔记详情失败: {e}")
        return Fail(message=f"获取小红书笔记详情失败: {str(e)}")

@router.post("/jizhiliao/search", summary="极致聊指数搜索")
async def search_jizhiliao_index(request: JizhiliaoSearchRequest):
    """执行极致聊指数搜索"""
    try:
        params = {
            "keyword": request.keyword,
            "page": request.page,
            "size": request.size
        }
        
        result = await third_party_api_controller.search_jizhiliao_index(params)
        
        if result.success:
            return Success(data=result.data, message=result.message)
        else:
            return Fail(message=result.message)
    except Exception as e:
        logger.error(f"极致聊指数搜索失败: {e}")
        return Fail(message=f"极致聊指数搜索失败: {str(e)}")

@router.post("/dify/workflows/run", summary="运行Dify工作流")
async def run_dify_workflow(request: DifyWorkflowRequest):
    """运行Dify工作流"""
    try:
        result = await third_party_api_controller.run_dify_workflow(
            zl_img=request.zl_img,
            response_mode=request.response_mode,
            user=request.user
        )
        
        if result.success:
            return Success(data=result.data, message=result.message)
        else:
            return Fail(message=result.message)
    except Exception as e:
        logger.error(f"运行Dify工作流失败: {e}")
        return Fail(message=f"运行Dify工作流失败: {str(e)}")