"""Notifications inbox API.""" from __future__ import annotations from typing import Optional from fastapi import APIRouter, HTTPException from pydantic import BaseModel from core.notifications.deadline_alerts import send_deadline_alert_email from core.notifications.inbox import ( create_post_award_deadline_notification, list_notifications, mark_read, ) router = APIRouter(prefix="/api/notifications", tags=["notifications"]) class DeadlineAlertTestBody(BaseModel): email: str project_id: str milestone: str deadline: str user_id: Optional[str] = "default" @router.get("/inbox") async def inbox(user_id: str = "default", unread_only: bool = False): return {"notifications": list_notifications(user_id, unread_only=unread_only)} @router.post("/{notification_id}/read") async def read_notification(notification_id: str): updated = mark_read(notification_id) if not updated: raise HTTPException(status_code=404, detail="Powiadomienie nie istnieje") return updated @router.post("/test-deadline-alert") async def test_deadline_alert(body: DeadlineAlertTestBody): note = create_post_award_deadline_notification( project_id=body.project_id, milestone=body.milestone, deadline_iso=body.deadline, user_id=body.user_id or "default", ) email_result = send_deadline_alert_email( to_email=str(body.email), subject=note["title"], body=f"{note['body']}\nTermin: {body.deadline}", ) return {"notification": note, "email": email_result}