from fastapi import APIRouter, Depends, status
from sqlalchemy.orm import Session

from app.api.v1.deps import get_current_user
from app.db.session import get_db
from app.models.user import User
from app.schemas.notification import DeviceTokenRegisterRequest
from app.services import push as push_service

router = APIRouter(prefix="/notifications", tags=["notifications"])


@router.post("/device-tokens", status_code=status.HTTP_204_NO_CONTENT)
def register_device_token(
    payload: DeviceTokenRegisterRequest,
    db: Session = Depends(get_db),
    current_user: User = Depends(get_current_user),
) -> None:
    """Any authenticated role can register a device — customers,
    couriers, and supplier/manager staff all receive push
    notifications (see app.services.order_notifications)."""
    push_service.register_device_token(
        db, current_user.id, payload.token, payload.platform
    )


@router.delete("/device-tokens/{token}", status_code=status.HTTP_204_NO_CONTENT)
def unregister_device_token(
    token: str,
    db: Session = Depends(get_db),
    current_user: User = Depends(get_current_user),
) -> None:
    """Call on logout so a shared/reinstalled device stops receiving
    notifications meant for the account that just signed out. Scoped
    to the caller's own token — a no-op, not an error, if it doesn't
    match one of theirs."""
    push_service.unregister_device_token(db, current_user.id, token)
