from __future__ import annotations

from pathlib import Path

from app.modules.orion.assets import schubamed_logo_uri
from app.modules.orion.context import ReportContext
from app.modules.orion.html import text


def render_report_chrome(context: ReportContext, logo_uri: str) -> str:
    report_number = text(context.validation.report_number)
    version = text(context.validation.version)
    report_date = text(context.validation.updated_at)
    return f"""
    <header class="report-header" aria-label="Berichtskopf">
      <div class="report-header-brand">
        <img src="{logo_uri}" alt="SCHUBAMED">
        <div>
          <strong>SCHUBAMED®</strong>
          <span>Aufbereitung mit System</span>
        </div>
      </div>
      <dl class="report-header-meta">
        <div><dt>Berichtsnummer</dt><dd>{report_number}</dd></div>
        <div><dt>Version</dt><dd>{version}</dd></div>
        <div><dt>Datum</dt><dd>{report_date}</dd></div>
      </dl>
    </header>
    <footer class="report-footer" aria-label="Berichtsfuß">
      <span>Validation Suite</span>
      <span>Seite <span class="page-number"></span> von <span class="page-count"></span></span>
      <span>Version {version}</span>
    </footer>
    """


def render_document(context: ReportContext, chapters: list[str]) -> str:
    css = (Path(__file__).resolve().parent / "report.css").read_text(encoding="utf-8")
    logo_uri = schubamed_logo_uri()
    title = f"Validierungsbericht {context.validation.report_number}"
    cover_markup = chapters[0] if chapters else ""
    content_markup = "\n".join(chapters[1:])
    return f"""<!doctype html>
<html lang="de">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>{text(title)}</title>
  <style>{css}</style>
</head>
<body>
  <article class="report-document">
    {cover_markup}
    <section class="report-content">
      <div class="report-meta" data-report-number="{text(context.validation.report_number)}" data-report-version="{text(context.validation.version)}" data-report-date="{text(context.validation.updated_at)}"></div>
      {render_report_chrome(context, logo_uri)}
      {content_markup}
    </section>
  </article>
</body>
</html>"""
