from uuid import UUID

from fastapi import APIRouter, Depends, Header

from app.api.deps import AsyncSession, get_db, require_tenant
from app.schemas.common import WebhookIn, WebhookOut
from app.services.webhook_service import WebhookService

router = APIRouter()


@router.post("/incoming", response_model=WebhookOut, status_code=201)
async def incoming_webhook(
    body: WebhookIn,
    tenant_id: UUID = Depends(require_tenant),
    session: AsyncSession = Depends(get_db),
    x_webhook_secret: str | None = Header(default=None),
):
    data = body.model_dump()
    return await WebhookService(session).receive(
        tenant_id, data, webhook_secret=x_webhook_secret
    )
