from fastapi import APIRouter, HTTPException, Query

from app.core.config import settings
from app.core.email import memory_mailer

router = APIRouter()


@router.get("/emails/latest-token")
async def latest_email_token(to: str = Query(...), template: str = Query(...)):
    if not settings.enable_test_routes or settings.email_delivery_mode != "memory":
        raise HTTPException(status_code=404, detail="NOT_FOUND")
    token = memory_mailer.latest_token(to, template)
    if not token:
        raise HTTPException(status_code=404, detail="TOKEN_NOT_FOUND")
    return {"token": token}
