from fastapi import APIRouter, Depends, Query
from sqlalchemy.orm import Session

from app.api.deps import get_current_user
from app.db.session import get_db
from app.schemas.notifications import (
    NotificationItem,
    NotificationListResponse,
    NotificationPreferencesResponse,
    NotificationPreferencesUpdate,
    RegisterDeviceRequest,
    UnreadCountResponse,
)
from app.schemas.user_context import CurrentUser
from app.services.notification_service import NotificationService

router = APIRouter()


@router.get("", response_model=NotificationListResponse)
def list_notifications(
    unread_only: bool = False,
    page: int = Query(1, ge=1),
    page_size: int = Query(20, ge=1, le=100),
    current_user: CurrentUser = Depends(get_current_user),
    db: Session = Depends(get_db),
):
    return NotificationService(db).list_notifications(
        current_user.id,
        unread_only=unread_only,
        page=page,
        page_size=page_size,
    )


@router.get("/unread-count", response_model=UnreadCountResponse)
def unread_count(
    current_user: CurrentUser = Depends(get_current_user),
    db: Session = Depends(get_db),
):
    count = NotificationService(db).unread_count(current_user.id)
    return UnreadCountResponse(unread_count=count)


@router.patch("/{notification_id}/read", response_model=NotificationItem)
def mark_notification_read(
    notification_id: int,
    current_user: CurrentUser = Depends(get_current_user),
    db: Session = Depends(get_db),
):
    return NotificationService(db).mark_read(current_user.id, notification_id)


@router.post("/read-all")
def mark_all_read(
    current_user: CurrentUser = Depends(get_current_user),
    db: Session = Depends(get_db),
):
    return NotificationService(db).mark_all_read(current_user.id)


@router.get("/preferences", response_model=NotificationPreferencesResponse)
def get_preferences(
    current_user: CurrentUser = Depends(get_current_user),
    db: Session = Depends(get_db),
):
    return NotificationService(db).get_preferences(current_user.id)


@router.put("/preferences", response_model=NotificationPreferencesResponse)
def update_preferences(
    payload: NotificationPreferencesUpdate,
    current_user: CurrentUser = Depends(get_current_user),
    db: Session = Depends(get_db),
):
    return NotificationService(db).update_preferences(current_user.id, payload)


@router.post("/devices")
def register_device(
    payload: RegisterDeviceRequest,
    current_user: CurrentUser = Depends(get_current_user),
    db: Session = Depends(get_db),
):
    return NotificationService(db).register_device(
        current_user.id,
        payload.platform,
        payload.token,
        payload.device_name,
    )


@router.delete("/devices")
def unregister_device(
    token: str = Query(..., min_length=10),
    current_user: CurrentUser = Depends(get_current_user),
    db: Session = Depends(get_db),
):
    return NotificationService(db).unregister_device(current_user.id, token)


@router.post("/generate")
def generate_notifications_now(
    current_user: CurrentUser = Depends(get_current_user),
    db: Session = Depends(get_db),
):
    if current_user.role != "admin":
        from app.core.exceptions import ForbiddenError

        raise ForbiddenError("Only admins can trigger notification generation")
    return NotificationService(db).generate_daily_notifications()
