from typing import Any

from fastapi import Request
from fastapi.templating import Jinja2Templates

from folkugat_web.config import api as api_config
from folkugat_web.config import directories as directories_config

templates = Jinja2Templates(directory=directories_config.TEMPLATES_DIR)


def url_for(request: Request, name: str, **path_params: Any) -> str:
    http_url = request.url_for(name, **path_params)
    if api_config.URL_SCHEME == "http":
        return str(http_url)
    # Replace 'http' with 'https'
    return str(http_url.replace(scheme=api_config.URL_SCHEME))


templates.env.globals["url_for"] = url_for
