#!/usr/bin/env python3
"""Synthetic browser fixture for Sprint 6I-A; contains no patient data."""
from __future__ import annotations
import io
import os
from pathlib import Path
import sys
from reportlab.pdfgen import canvas

ROOT=Path(__file__).resolve().parents[2]
HEALTH=ROOT/"scripts"/"health"
sys.path[:0]=[str(HEALTH),str(ROOT/"tests")]
from fixtures.dashboard_v5_fixture import build_dashboard_v5_fixture
from dashboard_v5.document_review import quarantine_upload
import health_dashboard_action_worker as worker
from health_dashboard_v5 import main as render_main


def pdf(text: str) -> bytes:
    output=io.BytesIO();document=canvas.Canvas(output,pagesize=(500,300));document.drawString(24,250,text);document.save();return output.getvalue()


def build(root: Path) -> None:
    root.mkdir(parents=True,exist_ok=True);database=root/"health.db";build_dashboard_v5_fixture(database)
    quarantine=root/"document-quarantine";storage=root/"documents"
    worker.DASHBOARD_DB=database;worker.DOCUMENT_QUARANTINE=quarantine;worker.DOCUMENT_STORAGE=storage;worker.DASHBOARD_V5_FILE=None
    common={"document_date":"2026-07-18","document_type":"doctor_report","institution":"Synthetic Clinic","personal_title":"Synthetic report","investigation_day":"2026-07-18","note":"synthetic"}
    for text in ("CRP 4.2 mg/l Referenz 0-5 mg/l. Diagnose: Synthetic alpha.","CRP 5.1 mg/l Referenz 0-5 mg/l. Diagnose: Synthetic alpha changed."):
        item=quarantine_upload(pdf(text),common,quarantine)
        worker.apply_document_import(worker.validate_document_action({"version":1,"action":"document_import","quarantine_token":item["token"],"sha256":item["sha256"],"metadata":common}))
    token=root/"api-token";token.write_text("browser-synthetic-token-6ia-1234567890\n");os.chmod(token,0o600)
    old=list(sys.argv);sys.argv=["health_dashboard_v5.py","--db",str(database),"--output",str(root/"dashboard-v5.html"),"--today","2026-07-19","--generated-at","2026-07-19T12:00:00+02:00","--health-record-6e"]
    try: render_main()
    finally: sys.argv=old


if __name__=="__main__": build(Path(sys.argv[1]).resolve())
