"""
Webhook 处理器
处理来自 Gitea 的 webhook 请求
"""

from fastapi import APIRouter, Depends, HTTPException, Request
from app.services.webhook_service import WebhookService
from app.services.dedup_service import DeduplicationService
from app.tasks.jenkins_tasks import get_celery_app

router = APIRouter()

def get_webhook_service() -> WebhookService:
    """获取 webhook 服务实例"""
    # 这里应该从依赖注入容器获取
    # 暂时返回 None，实际使用时需要正确实现
    return None

@router.post("/gitea")
async def handle_gitea_webhook(
    request: Request,
    webhook_service: WebhookService = Depends(get_webhook_service)
):
    """处理 Gitea webhook 请求"""
    if webhook_service is None:
        raise HTTPException(status_code=503, detail="Webhook service not available")
    
    try:
        # 获取请求体
        body = await request.body()
        
        # 处理 webhook
        result = await webhook_service.process_webhook(body, request.headers)
        
        return {
            "success": True,
            "message": "Webhook processed successfully",
            "data": result
        }
        
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e)) 