import logging
from datetime import datetime, timedelta, timezone
from fastapi import APIRouter, Request
from fastapi.responses import HTMLResponse, RedirectResponse
from devplacepy.database import get_table
from devplacepy.templating import templates
from devplacepy.utils import require_user, time_ago
from devplacepy.seo import base_seo_context

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


def _group_label(created_at: str) -> str:
    try:
        dt = datetime.fromisoformat(created_at)
        now = datetime.now(timezone.utc)
        today = now.date()
        date = dt.date()
        if date == today:
            return "Today"
        if date == today - timedelta(days=1):
            return "Yesterday"
        if (today - date).days < 7:
            return "This week"
        return "Older"
    except Exception:
        return "Older"


@router.get("", response_class=HTMLResponse)
async def notifications_page(request: Request):
    user = require_user(request)

    try:
        notifications_table = get_table("notifications")
        raw_notifications = list(
            notifications_table.find(user_uid=user["uid"], order_by=["-created_at"])
        )

        enriched = []
        if raw_notifications:
            from devplacepy.database import get_users_by_uids
            actor_uids = [n.get("related_uid") for n in raw_notifications if n.get("related_uid")]
            actors = get_users_by_uids(actor_uids)
        else:
            actors = {}
        for n in raw_notifications:
            enriched.append({
                "notification": n,
                "actor": actors.get(n.get("related_uid")),
                "time_ago": time_ago(n["created_at"]),
            })

        groups = []
        current_label = None
        current_entries = []
        for item in enriched:
            label = _group_label(item["notification"]["created_at"])
            if label != current_label:
                if current_entries:
                    groups.append({"label": current_label, "entries": current_entries})
                current_label = label
                current_entries = []
            current_entries.append(item)
        if current_entries:
            groups.append({"label": current_label, "entries": current_entries})
    except Exception as e:
        logger.exception(f"Error loading notifications: {e}")
        groups = []

    seo_ctx = base_seo_context(
        request,
        title="Notifications",
        robots="noindex,nofollow",
        breadcrumbs=[
            {"name": "Home", "url": "/feed"},
            {"name": "Notifications", "url": "/notifications"},
        ],
    )
    return templates.TemplateResponse(request, "notifications.html", {
        **seo_ctx,
        "request": request,
        "user": user,
        "notification_groups": groups,
    })


@router.post("/mark-read/{notification_uid}")
async def mark_read(request: Request, notification_uid: str):
    user = require_user(request)
    notifications_table = get_table("notifications")
    n = notifications_table.find_one(uid=notification_uid)
    if n and n["user_uid"] == user["uid"]:
        notifications_table.update({"id": n["id"], "read": True}, ["id"])
    return RedirectResponse(url="/notifications", status_code=302)


@router.post("/mark-all-read")
async def mark_all_read(request: Request):
    user = require_user(request)
    notifications_table = get_table("notifications")
    for n in notifications_table.find(user_uid=user["uid"], read=False):
        notifications_table.update({"id": n["id"], "read": True}, ["id"])
    return RedirectResponse(url="/notifications", status_code=302)
