from fastapi import APIRouter, Depends, Query, status
from sqlalchemy.orm import Session
from typing import Optional, List
from utils.utils import get_db
from . import handler
from .schema import NotificationCreate, NotificationUpdate, NotificationResponse

router = APIRouter(prefix="/notifications", tags=["Notifications"])


@router.post(
    "/",
    response_model=NotificationResponse,
    status_code=status.HTTP_201_CREATED,
    summary="Create Notification",
    description="Creates a new notification for a user. Validates that the user exists before creating the notification.",
)
def create_notification_route(data: NotificationCreate, db: Session = Depends(get_db)):
    return handler.create_notification(db, data)


@router.get(
    "/",
    response_model=List[NotificationResponse],
    summary="List Notifications",
    description="Returns a paginated list of notifications. Supports filtering by user_id, is_read status, and notification_type. Results are ordered by creation date descending.",
)
def list_notifications_route(
    limit: int = Query(20, ge=1, le=100),
    offset: int = Query(0, ge=0),
    user_id: Optional[str] = Query(None, min_length=36, max_length=36),
    is_read: Optional[bool] = None,
    notification_type: Optional[str] = None,
    db: Session = Depends(get_db),
):
    return handler.list_notifications(db, limit, offset, user_id, is_read, notification_type)


@router.get(
    "/{notification_id}",
    response_model=NotificationResponse,
    summary="Get Notification",
    description="Retrieves a single notification by ID. Returns 404 if the notification does not exist.",
)
def get_notification_route(notification_id: str, db: Session = Depends(get_db)):
    return handler.get_notification(db, notification_id)


@router.put(
    "/{notification_id}",
    response_model=NotificationResponse,
    summary="Update Notification",
    description="Updates an existing notification. All fields are optional. Returns 404 if the notification does not exist.",
)
def update_notification_route(
    notification_id: str, data: NotificationUpdate, db: Session = Depends(get_db)
):
    return handler.update_notification(db, notification_id, data)


@router.delete(
    "/{notification_id}",
    status_code=status.HTTP_200_OK,
    summary="Delete Notification",
    description="Deletes a notification by ID. Returns 404 if the notification does not exist.",
)
def delete_notification_route(notification_id: str, db: Session = Depends(get_db)):
    return handler.delete_notification(db, notification_id)


@router.post(
    "/{notification_id}/mark-read",
    response_model=NotificationResponse,
    summary="Mark Notification As Read",
    description="Marks a single notification as read and sets the read_at timestamp. Returns 404 if the notification does not exist.",
)
def mark_notification_as_read_route(notification_id: str, db: Session = Depends(get_db)):
    return handler.mark_notification_as_read(db, notification_id)


@router.post(
    "/users/{user_id}/mark-all-read",
    status_code=status.HTTP_200_OK,
    summary="Mark All Notifications As Read",
    description="Marks all unread notifications for a specific user as read. Returns the count of notifications marked as read. Returns 400 if the user does not exist.",
)
def mark_all_notifications_as_read_route(user_id: str, db: Session = Depends(get_db)):
    return handler.mark_all_notifications_as_read(db, user_id)


@router.get(
    "/users/{user_id}/unread-count",
    status_code=status.HTTP_200_OK,
    summary="Get Unread Notification Count",
    description="Returns the count of unread notifications for a specific user. Returns 400 if the user does not exist.",
)
def get_unread_notification_count_route(user_id: str, db: Session = Depends(get_db)):
    return handler.get_unread_notification_count(db, user_id)