import re
from pathlib import Path

from src.views import dashboard as dashboard_html


FRONTEND = Path(__file__).resolve().parents[1] / "frontend"
PAGE_SCRIPT_MANIFEST = "\n".join(
    re.findall(
        r'^<script src="static/[^\"]+\.js"></script>$',
        (FRONTEND / "index.html").read_text(),
        re.MULTILINE,
    )
)


def dashboard_bundle_text() -> str:
    """Return the HTML, CSS, and bootstrap sources that make up the dashboard."""
    return "\n".join(
        (
            (FRONTEND / "index.html").read_text(),
            (FRONTEND / "dashboard.css").read_text(),
            (FRONTEND / "dashboard.js").read_text(),
        )
    )


async def dashboard() -> str:
    # Exercise the real view lookup before adding its separately served bootstrap assets.
    html = await dashboard_html()
    return "\n".join(
        (
            html,
            PAGE_SCRIPT_MANIFEST,
            (FRONTEND / "dashboard.css").read_text(),
            (FRONTEND / "dashboard.js").read_text(),
        )
    )
