import logging
from typing import Dict, Any, List, Optional

from app.utils.universal_api_manager import universal_api
from app.schemas.third_party_api import (
    APIProviderInfo, 
    APIEndpointInfo, 
    APIResponse
)

logger = logging.getLogger(__name__)


class ThirdPartyAPIController:
    """第三方API控制器"""
    
    def __init__(self):
        self.api_manager = universal_api
    
    async def make_api_request(
        self, 
        provider: str, 
        endpoint: str, 
        params: Dict[str, Any] = None,
        timeout: int = 30
    ) -> APIResponse:
        """通用API请求方法"""
        try:
            result = self.api_manager.make_request(
                provider=provider,
                endpoint=endpoint,
                params=params or {},
                timeout=timeout
            )
            
            return APIResponse(
                success=True,
                data=result,
                message="请求成功",
                provider=provider,
                endpoint=endpoint
            )
        except Exception as e:
            logger.error(f"API请求失败: {e}")
            return APIResponse(
                success=False,
                data={},
                message=f"请求失败: {str(e)}",
                provider=provider,
                endpoint=endpoint
            )
    
    # 站长之家API便捷方法
    async def query_copyright_software(self, company_name: str, chinaz_ver: str = "1") -> APIResponse:
        """查询企业软件著作权"""
        return await self.make_api_request(
            provider="chinaz",
            endpoint="copyright_software",
            params={
                "entName": company_name,
                "pageNo": 1,
                "range": 100,
                "ChinazVer": chinaz_ver
            }
        )
    
    async def query_patent_info(self, company_name: str, chinaz_ver: str = "1") -> APIResponse:
        """查询企业专利信息"""
        return await self.make_api_request(
            provider="chinaz",
            endpoint="patent",
            params={
                "searchKey": company_name,
                "pageNo": 1,
                "range": 100,
                "searchType": "0",
                "ChinazVer": chinaz_ver
            }
        )

    async def query_judicial_data(self, company_name: str, chinaz_ver: str = "1") -> APIResponse:
        """查询司法综合数据"""
        return await self.make_api_request(
            provider="chinaz",
            endpoint="judgement",
            params={
                "q": company_name,
                "pageNo": 1,
                "ChinazVer": chinaz_ver
            }
        )
    
    # 小红书API便捷方法
    async def get_xiaohongshu_note(self, note_id: str) -> APIResponse:
        """获取小红书笔记详情"""
        params = {"noteId": note_id}
            
        return await self.make_api_request(
            provider="xiaohongshu",
            endpoint="xiaohongshu_note_detail",
            params=params
        )

    async def search_jizhiliao_index(self, params: Dict[str, Any], timeout: int = 30) -> APIResponse:
        """执行极致聊指数搜索"""
        return await self.make_api_request(
            provider="jizhiliao",
            endpoint="index_search",
            params=params,
            timeout=timeout
        )


# 创建全局控制器实例
third_party_api_controller = ThirdPartyAPIController()