# Copyright (c) conda-store development team. All rights reserved.
# Use of this source code is governed by a BSD-style
# license that can be found in the LICENSE file.

from fastapi import APIRouter, Depends, Request
from fastapi.responses import HTMLResponse

from conda_store_server._internal.server import dependencies

router_conda_store_ui = APIRouter(
    prefix="/ui",
    tags=["conda-store-ui"],
    # Provide the response class so that it can show up in the autogenerated API
    # docs. The docs show the endpoints as returning HTML rather than JSON.
    default_response_class=HTMLResponse,
)


# Yes, "{path:path}"" makes "/" redundant. But if we don't define "/", then we
# always have to pass `path` to the `url_for` function like so:
# url_for("get_conda_store_ui", path="")
@router_conda_store_ui.get("/")
@router_conda_store_ui.get("{path:path}")
async def get_conda_store_ui(
    request: Request,
    path: str,
    templates=Depends(dependencies.get_templates),
    url_prefix=Depends(dependencies.get_url_prefix),
):
    context = {
        "request": request,
        "url_prefix": url_prefix,
        "ui_prefix": router_conda_store_ui.prefix,
    }
    response = templates.TemplateResponse(request, "conda-store-ui.html", context)
    if path.endswith("not-found"):
        response.status_code = 404
    return response
