from uuid import UUID from fastapi import APIRouter, Depends from app.api.deps import AsyncSession, CurrentUser, get_current_user, get_db, require_tenant from app.schemas.common import ( TemplateCreate, TemplateOut, TemplatePreviewRequest, TemplatePreviewResponse, ) from app.services.template_service import TemplateService router = APIRouter() @router.get("", response_model=list[TemplateOut]) async def list_templates( tenant_id: UUID = Depends(require_tenant), session: AsyncSession = Depends(get_db), _: CurrentUser = Depends(get_current_user), ): return await TemplateService(session).list(tenant_id) @router.post("", response_model=TemplateOut, status_code=201) async def create_template( body: TemplateCreate, tenant_id: UUID = Depends(require_tenant), session: AsyncSession = Depends(get_db), user: CurrentUser = Depends(get_current_user), ): return await TemplateService(session).create( tenant_id, body.model_dump(), actor_id=user.user_id ) @router.get("/{template_id}", response_model=TemplateOut) async def get_template( template_id: UUID, tenant_id: UUID = Depends(require_tenant), session: AsyncSession = Depends(get_db), _: CurrentUser = Depends(get_current_user), ): return await TemplateService(session).get(tenant_id, template_id) @router.post("/{template_id}/submit", response_model=TemplateOut) async def submit_template( template_id: UUID, tenant_id: UUID = Depends(require_tenant), session: AsyncSession = Depends(get_db), user: CurrentUser = Depends(get_current_user), ): return await TemplateService(session).submit_for_approval( tenant_id, template_id, actor_id=user.user_id ) @router.post("/{template_id}/approve", response_model=TemplateOut) async def approve_template( template_id: UUID, tenant_id: UUID = Depends(require_tenant), session: AsyncSession = Depends(get_db), user: CurrentUser = Depends(get_current_user), ): return await TemplateService(session).approve( tenant_id, template_id, actor_id=user.user_id ) @router.post("/{template_id}/reject", response_model=TemplateOut) async def reject_template( template_id: UUID, tenant_id: UUID = Depends(require_tenant), session: AsyncSession = Depends(get_db), user: CurrentUser = Depends(get_current_user), reason: str | None = None, ): return await TemplateService(session).reject( tenant_id, template_id, reason=reason, actor_id=user.user_id ) @router.post("/{template_id}/preview", response_model=TemplatePreviewResponse) async def preview_template( template_id: UUID, body: TemplatePreviewRequest, tenant_id: UUID = Depends(require_tenant), session: AsyncSession = Depends(get_db), _: CurrentUser = Depends(get_current_user), ): result = await TemplateService(session).preview( tenant_id, template_id, body.variables ) return TemplatePreviewResponse(**result)