from typing import Annotated
from fastapi import APIRouter, Cookie
from fastapi.templating import Jinja2Templates

from rvpc.auth.device import MatchingDevice
from rvpc.auth.tokens.capture_tokens import ValidCaptureTokenInfo
from rvpc.routers.utils import smart


def make_nav(prefix: str, templates: Jinja2Templates):
    nav = APIRouter(prefix=prefix)

    @nav.get("/")
    @smart("/nav/nav.html")
    async def render_device(
        matching_device: MatchingDevice,
        token_info: ValidCaptureTokenInfo,
        location: Annotated[str | None, Cookie()] = None,
    ):
        context = {"location": location, "device_enabled": False}

        if (
            matching_device is not None
            and location is not None
            and token_info is not None
        ):
            context["device_enabled"] = True
        return context

    return nav
