"""
Notification management endpoints for staff monitoring and manual dispatch.
"""

from typing import Annotated
from uuid import UUID

from fastapi import APIRouter, Depends, Query, status
from sqlalchemy.ext.asyncio import AsyncSession

from app.core.auth_deps import require_permission
from app.core.dependencies import get_db
from app.core.pagination import PaginationParams
from app.core.permissions import PermissionCode
from app.core.responses import PaginatedResponse, SuccessResponse
from app.models.notification import NotificationChannel, NotificationStatus
from app.models.user import User
from app.schemas.notification import NotificationResponse, NotificationSendRequest
from app.services.notification_service import NotificationService

router = APIRouter(prefix="/notifications", tags=["Notifications & Comms"])
_notification_service = NotificationService()


@router.get("", response_model=PaginatedResponse[NotificationResponse])
async def list_notifications(
    db: AsyncSession = Depends(get_db),
    current_user: User = Depends(require_permission(PermissionCode.NOTIFICATION_READ)),
    pagination: PaginationParams = Depends(),
    status_filter: Annotated[NotificationStatus | None, Query(alias="status")] = None,
    channel: Annotated[NotificationChannel | None, Query()] = None,
    patient_id: Annotated[UUID | None, Query()] = None,
) -> PaginatedResponse[NotificationResponse]:
    """
    List dispatched notifications with branch scoping and optional filters.
    Requires `notification:read` permission.
    """
    return await _notification_service.list_notifications(
        db=db,
        current_user=current_user,
        pagination=pagination,
        status=status_filter,
        channel=channel,
        patient_id=patient_id,
    )


@router.get("/{notification_id}", response_model=SuccessResponse[NotificationResponse])
async def get_notification(
    notification_id: UUID,
    db: AsyncSession = Depends(get_db),
    current_user: User = Depends(require_permission(PermissionCode.NOTIFICATION_READ)),
) -> SuccessResponse[NotificationResponse]:
    """
    Retrieve single notification audit record by ID.
    Requires `notification:read` permission.
    """
    notif = await _notification_service.get_notification(db, notification_id, current_user)
    return SuccessResponse(data=notif)


@router.post(
    "/send",
    response_model=SuccessResponse[NotificationResponse],
    status_code=status.HTTP_201_CREATED,
)
async def send_custom_notification(
    data: NotificationSendRequest,
    db: AsyncSession = Depends(get_db),
    current_user: User = Depends(require_permission(PermissionCode.NOTIFICATION_WRITE)),
) -> SuccessResponse[NotificationResponse]:
    """
    Trigger manual staff notification to a patient.
    Requires `notification:write` permission.
    """
    res = await _notification_service.send_custom_notification(db, data, current_user)
    return SuccessResponse(data=res, message="Notification dispatched successfully.")
