from fastapi import APIRouter, Depends, HTTPException
from typing import Dict, Any, List

from app.services.comparison_service import ComparisonService
from app.routes.user import get_current_active_user

# 创建路由器
router = APIRouter(prefix="/comparison", tags=["comparison"])

# 创建对比服务实例
comparison_service = ComparisonService()


@router.post("/compare-algorithms", response_model=dict)
async def compare_algorithms(
    request_data: Dict[str, Any],
    current_user: dict = Depends(get_current_active_user)
):
    """比较多个算法的效果
    
    Args:
        request_data: 请求数据，包含input_data和algorithm_configs
        current_user: 当前活跃用户
        
    Returns:
        对比结果
    """
    input_data = request_data.get("input_data")
    algorithm_configs = request_data.get("algorithm_configs")
    
    if not input_data:
        raise HTTPException(status_code=400, detail="缺少 input_data 参数")
    
    if not algorithm_configs or not isinstance(algorithm_configs, list):
        raise HTTPException(status_code=400, detail="缺少 algorithm_configs 参数或格式错误")
    
    result = await comparison_service.compare_algorithms(input_data, algorithm_configs)
    
    if not result["success"]:
        raise HTTPException(status_code=500, detail=result.get("error", "对比失败"))
    
    return result


@router.post("/generate-report", response_model=dict)
async def generate_comparison_report(
    comparison_results: Dict[str, Any],
    current_user: dict = Depends(get_current_active_user)
):
    """生成对比报告
    
    Args:
        comparison_results: 对比结果
        current_user: 当前活跃用户
        
    Returns:
        对比报告
    """
    report = comparison_service.generate_comparison_report(comparison_results)
    
    if not report["success"]:
        raise HTTPException(status_code=500, detail=report.get("error", "生成报告失败"))
    
    return report
