"""Notification routes.""" from fastapi import APIRouter, Depends, HTTPException, status, Query from sqlalchemy.orm import Session from typing import List, Optional from request_app.core.database import get_db from request_app.core.auth import get_current_user_id from request_app.schemas.reference import NotificationResponse, NotificationListResponse from request_app.services.notification_service import NotificationService router = APIRouter(prefix="/notifications", tags=["Notifications"]) @router.get("", response_model=NotificationListResponse) async def get_notifications( is_read: Optional[int] = Query(None, description="Filter by read status (0 or 1)"), token: str = Depends(get_current_user_id), db: Session = Depends(get_db) ): """Get current user's notifications.""" service = NotificationService(db) notifications = service.get_user_notifications(token, is_read) # Count unread unread_count = sum(1 for n in notifications if n.is_read == 0) return NotificationListResponse( notifications=notifications, total=len(notifications), unread_count=unread_count ) @router.put("/{notification_id}/read", response_model=NotificationResponse) async def mark_as_read( notification_id: int, token: str = Depends(get_current_user_id), db: Session = Depends(get_db) ): """Mark notification as read.""" service = NotificationService(db) try: return service.mark_as_read(notification_id, token) except ValueError as e: raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=str(e)) @router.put("/read-all") async def mark_all_as_read( token: str = Depends(get_current_user_id), db: Session = Depends(get_db) ): """Mark all notifications as read.""" service = NotificationService(db) updated = service.mark_all_as_read(token) return {"message": f"Marked {updated} notifications as read"} @router.delete("/{notification_id}", status_code=status.HTTP_204_NO_CONTENT) async def delete_notification( notification_id: int, token: str = Depends(get_current_user_id), db: Session = Depends(get_db) ): """Delete notification.""" service = NotificationService(db) if not service.delete_notification(notification_id, token): raise HTTPException( status_code=status.HTTP_404_NOT_FOUND, detail="Notification not found" )