from fastapi import APIRouter, BackgroundTasks, Depends
from typing import Dict, Any
 
from app.services.job import create_data_router_service, PlatformType, DataType
from app.core.clickhouse import clickhouse_manager
from app.controllers.job import (
    UniversalDataController, 
    UniversalDataRequest, 
    BatchDataRequest,
    create_universal_data_controller
)


router = APIRouter(tags=["通用数据接口"])


async def get_universal_data_controller() -> UniversalDataController:
    """获取通用数据控制器实例"""
    clickhouse_client = await clickhouse_manager.get_client()
    data_router_service = create_data_router_service(clickhouse_client)
    return create_universal_data_controller(data_router_service)


@router.post("/data/store", summary="存储单条数据")
async def store_single_data(
    request: UniversalDataRequest,
    controller: UniversalDataController = Depends(get_universal_data_controller)
) -> Dict[str, Any]:
    """
    通用数据存储接口 - 存储单条数据
    
    支持的平台:
    - boss: Boss直聘
    - qcwy: 前程无忧
    - zhilian: 智联招聘
    
    支持的数据类型:
    - job: 职位数据
    - company: 公司数据
    
    示例请求:
    ```json
    {
        "data": {
            "jobBaseInfoVO": {
                "encryptJobId": "abc123",
                "positionName": "Python开发工程师",
                "locationName": "北京"
            },
            "brandComInfoVO": {
                "brandName": "某科技公司",
                "industryName": "互联网"
            }
        },
        "data_type": "job",
        "platform": "boss",
        "check_duplicate": true,
        "duplicate_key": "encrypt_job_id"
    }
    ```
    """
    return await controller.store_single_data(request)


@router.post("/data/batch-store", summary="批量存储数据")
async def store_batch_data(
    request: BatchDataRequest,
    controller: UniversalDataController = Depends(get_universal_data_controller)
) -> Dict[str, Any]:
    """
    通用数据存储接口 - 批量存储数据
    
    示例请求:
    ```json
    {
        "data_list": [
            {
                "jobBaseInfoVO": {
                    "encryptJobId": "abc123",
                    "positionName": "Python开发工程师"
                }
            },
            {
                "jobBaseInfoVO": {
                    "encryptJobId": "def456",
                    "positionName": "Java开发工程师"
                }
            }
        ],
        "data_type": "job",
        "platform": "boss",
        "check_duplicate": true
    }
    ```
    """
    return await controller.store_batch_data(request)


@router.post("/data/store-async", summary="异步存储单条数据")
async def store_single_data_async(
    request: UniversalDataRequest,
    background_tasks: BackgroundTasks,
    controller: UniversalDataController = Depends(get_universal_data_controller)
) -> Dict[str, Any]:
    """
    通用数据存储接口 - 异步存储单条数据
    
    适用于大量数据或不需要立即返回结果的场景
    """
    return await controller.store_single_data_async(background_tasks, request)


@router.post("/data/batch-store-async", summary="异步批量存储数据")
async def store_batch_data_async(
    request: BatchDataRequest,
    background_tasks: BackgroundTasks,
    controller: UniversalDataController = Depends(get_universal_data_controller)
) -> Dict[str, Any]:
    """
    通用数据存储接口 - 异步批量存储数据
    
    适用于大批量数据处理场景
    """
    return await controller.store_batch_data_async(background_tasks, request)


@router.get("/data", summary="查询数据")
async def query_data(
    platform: str,
    data_type: str,
    page: int = 1,
    page_size: int = 20,
    controller: UniversalDataController = Depends(get_universal_data_controller)
) -> Dict[str, Any]:
    """
    通用数据查询接口
    
    参数:
    - platform: 平台类型 (boss/qcwy/zhilian)
    - data_type: 数据类型 (job/company)
    - page: 页码，默认1
    - page_size: 每页大小，默认20
    """
    # 转换字符串参数为枚举类型
    platform_enum = PlatformType(platform)
    data_type_enum = DataType(data_type)
    return await controller.query_data(platform_enum, data_type_enum, page, page_size)

@router.get("/platforms", summary="获取支持的平台和数据类型")
async def get_supported_platforms(
    controller: UniversalDataController = Depends(get_universal_data_controller)
) -> Dict[str, Any]:
    """
    获取支持的平台和数据类型信息
    
    返回:
    - 支持的平台列表
    - 支持的数据类型列表
    - 各平台的默认重复检查字段
    """
    return await controller.get_supported_platforms()


# 为了兼容性，提供平台特定的路由别名
@router.post("/boss/job", summary="Boss直聘职位数据存储")
async def store_boss_job_data(
    data: Dict[str, Any],
    controller: UniversalDataController = Depends(get_universal_data_controller)
) -> Dict[str, Any]:
    """Boss直聘职位数据存储的便捷接口"""
    request = UniversalDataRequest(
        data=data,
        data_type="job",
        platform="boss",
        check_duplicate=True,
    )
    return await controller.store_single_data(request)


@router.post("/boss/company", summary="Boss直聘公司数据存储")
async def store_boss_company_data(
    data: Dict[str, Any],
    controller: UniversalDataController = Depends(get_universal_data_controller)
) -> Dict[str, Any]:
    """Boss直聘公司数据存储的便捷接口"""
    request = UniversalDataRequest(
        data=data,
        data_type="company",
        platform="boss",
        check_duplicate=True,
    )
    return await controller.store_single_data(request)


@router.post("/qcwy/job", summary="前程无忧职位数据存储")
async def store_qcwy_job_data(
    data: Dict[str, Any],
    controller: UniversalDataController = Depends(get_universal_data_controller)
) -> Dict[str, Any]:
    """前程无忧职位数据存储的便捷接口"""
    request = UniversalDataRequest(
        data=data,
        data_type="job",
        platform="qcwy",
        check_duplicate=True,
    )
    return await controller.store_single_data(request)


@router.post("/qcwy/company", summary="前程无忧公司数据存储")
async def store_qcwy_company_data(
    data: Dict[str, Any],
    controller: UniversalDataController = Depends(get_universal_data_controller)
) -> Dict[str, Any]:
    """前程无忧公司数据存储的便捷接口"""
    request = UniversalDataRequest(
        data=data,
        data_type="company",
        platform="qcwy",
        check_duplicate=True,
    )
    return await controller.store_single_data(request)


@router.post("/zhilian/job", summary="智联招聘职位数据存储")
async def store_zhilian_job_data(
    data: Dict[str, Any],
    controller: UniversalDataController = Depends(get_universal_data_controller)
) -> Dict[str, Any]:
    """智联招聘职位数据存储的便捷接口"""
    request = UniversalDataRequest(
        data=data,
        data_type="job",
        platform="zhilian",
        check_duplicate=True,
    )
    return await controller.store_single_data(request)


@router.post("/zhilian/company", summary="智联招聘公司数据存储")
async def store_zhilian_company_data(
    data: Dict[str, Any],
    controller: UniversalDataController = Depends(get_universal_data_controller)
) -> Dict[str, Any]:
    """智联招聘公司数据存储的便捷接口"""
    request = UniversalDataRequest(
        data=data,
        data_type="company",
        platform="zhilian",
        check_duplicate=True,
    )
    return await controller.store_single_data(request)