# retoor <retoor@molodetz.nl>

import logging

from fastapi import APIRouter, Request

from devplacepy.services.devrant.params import merge_params
from devplacepy.services.devrant.notifications import build_notif_feed, clear_notifications
from devplacepy.routers.devrant._shared import dr_ok, resolve_actor, unauthorized
from devplacepy.services.audit import record as audit

logger = logging.getLogger(__name__)
router = APIRouter()


@router.get("/users/me/notif-feed")
async def notif_feed(request: Request):
    params = await merge_params(request)
    user = resolve_actor(request, params)
    if not user:
        return unauthorized()
    return dr_ok(data=build_notif_feed(user))


@router.delete("/users/me/notif-feed")
async def clear_notif_feed(request: Request):
    params = await merge_params(request)
    user = resolve_actor(request, params)
    if not user:
        return unauthorized()
    clear_notifications(user)
    audit.record(
        request,
        "notification.read.all",
        user=user,
        origin="devrant",
        summary=f"{user['username']} cleared all notifications",
    )
    return dr_ok()
