from typing import List, Any from uuid import UUID from datetime import datetime from fastapi import APIRouter, Depends, HTTPException, status from sqlalchemy.ext.asyncio import AsyncSession from sqlalchemy import select, update, desc from core.deps import get_db, get_current_user from models.user import User from models.notification import Notification from schemas.notification import NotificationResponse router = APIRouter() @router.get("/", response_model=List[NotificationResponse]) async def list_notifications( skip: int = 0, limit: int = 50, current_user: User = Depends(get_current_user), db: AsyncSession = Depends(get_db) ) -> Any: """Fetch notifications for the current user, most recent first.""" stmt = select(Notification).where( Notification.user_id == current_user.id, Notification.workspace_id == current_user.workspace_id, Notification.deleted_at.is_(None) ).order_by(desc(Notification.created_at)).offset(skip).limit(limit) result = await db.execute(stmt) return result.scalars().all() @router.patch("/mark-read/", status_code=status.HTTP_200_OK) async def mark_notifications_read( current_user: User = Depends(get_current_user), db: AsyncSession = Depends(get_db) ) -> Any: """Mark all unread notifications as read for current user.""" stmt = update(Notification).where( Notification.user_id == current_user.id, Notification.is_read == False ).values(is_read=True) await db.execute(stmt) await db.commit() return {"status": "success"} @router.patch("/{notification_id}/read/", status_code=status.HTTP_200_OK) async def mark_notification_read( notification_id: UUID, current_user: User = Depends(get_current_user), db: AsyncSession = Depends(get_db) ) -> Any: """Mark a single notification as read.""" stmt = update(Notification).where( Notification.id == notification_id, Notification.user_id == current_user.id ).values(is_read=True) await db.execute(stmt) await db.commit() return {"status": "success"} @router.delete("/clear/", status_code=status.HTTP_200_OK) async def clear_all_notifications( current_user: User = Depends(get_current_user), db: AsyncSession = Depends(get_db) ) -> Any: """Soft-delete all notifications for the current user.""" stmt = update(Notification).where( Notification.user_id == current_user.id, Notification.workspace_id == current_user.workspace_id, Notification.deleted_at.is_(None) ).values(deleted_at=datetime.utcnow()) await db.execute(stmt) await db.commit() return {"status": "success", "detail": "All notifications cleared"} @router.delete("/{notification_id}/", status_code=status.HTTP_200_OK) async def delete_notification( notification_id: UUID, current_user: User = Depends(get_current_user), db: AsyncSession = Depends(get_db) ) -> Any: """Soft-delete a single notification.""" stmt = select(Notification).where( Notification.id == notification_id, Notification.user_id == current_user.id ) result = await db.execute(stmt) notif = result.scalar_one_or_none() if not notif: raise HTTPException(status_code=404, detail="Notification not found") notif.deleted_at = datetime.utcnow() db.add(notif) await db.commit() return {"status": "deleted"}