"""Notification routes.""" from __future__ import annotations import uuid from fastapi import APIRouter, Depends, HTTPException, Query, status from sqlalchemy.ext.asyncio import AsyncSession from app.core.db import get_db from app.core.notifications import ( get_unread_count, list_notifications, mark_notification_read, ) from app.deps import get_current_user router = APIRouter(prefix="/api/v1/notifications", tags=["notifications"]) @router.get("") async def list_notifications_endpoint( page: int = Query(1, ge=1), page_size: int = Query(25, ge=1, le=100), db: AsyncSession = Depends(get_db), current_user: dict = Depends(get_current_user), ): """List notifications (unread first).""" tenant_id = uuid.UUID(current_user["tenant_id"]) user_id = uuid.UUID(current_user["user_id"]) return await list_notifications(db, tenant_id, user_id, page, page_size) @router.patch("/{notification_id}/read") async def mark_notification_read_endpoint( notification_id: str, db: AsyncSession = Depends(get_db), current_user: dict = Depends(get_current_user), ): """Mark a notification as read.""" tenant_id = uuid.UUID(current_user["tenant_id"]) user_id = uuid.UUID(current_user["user_id"]) try: nid = uuid.UUID(notification_id) except ValueError: raise HTTPException(400, detail={"detail": "Invalid notification_id", "code": "invalid_id"}) notif = await mark_notification_read(db, tenant_id, user_id, nid) if notif is None: raise HTTPException(404, detail={"detail": "Notification not found", "code": "not_found"}) return { "id": str(notif.id), "type": notif.type, "title": notif.title, "body": notif.body, "read_at": notif.read_at.isoformat() if notif.read_at else None, "created_at": notif.created_at.isoformat() if notif.created_at else None, } @router.get("/unread-count") async def unread_count_endpoint( db: AsyncSession = Depends(get_db), current_user: dict = Depends(get_current_user), ): """Get unread notification count.""" tenant_id = uuid.UUID(current_user["tenant_id"]) user_id = uuid.UUID(current_user["user_id"]) count = await get_unread_count(db, tenant_id, user_id) return {"count": count}