from __future__ import annotations

import tempfile
import unittest
from pathlib import Path

from epione_templates import load_template_data
from epione_templates.web_app import render_web_app, write_web_app


class WebAppTests(unittest.TestCase):
    def test_rendered_app_contains_clinical_workspace(self) -> None:
        data = load_template_data()
        output = render_web_app(data)

        self.assertIn("Epione EMR Templates", output)
        self.assertIn('id="epione-data"', output)
        self.assertIn("Product Verification", output)
        self.assertIn("copyNote", output)
        self.assertIn("fillDemo", output)
        self.assertIn("Physician Review", output)
        self.assertIn("Guided Procedure Path", output)
        self.assertIn("Physician Favorites", output)
        self.assertIn("saveFavorite", output)
        self.assertIn("FAVORITE_STORAGE_KEY", output)
        self.assertIn("renderGuidedPath", output)
        self.assertIn("quick-choice-list", output)
        self.assertIn("data-preset-text", output)
        self.assertIn("copyReviewPacket", output)
        self.assertIn("reviewPacket", output)
        self.assertIn('<script id="epione-data" type="application/json">', output)
        self.assertIn("JSON.parse(document.getElementById('epione-data').textContent)", output)

    def test_write_web_app_outputs_single_standalone_html_file(self) -> None:
        data = load_template_data()

        with tempfile.TemporaryDirectory() as temp_dir:
            outputs = write_web_app(data, Path(temp_dir))
            app_path = Path(outputs["html_app"])
            self.assertTrue(app_path.exists())

        self.assertEqual(app_path.name, "Epione_Template_App_v0.7.html")


if __name__ == "__main__":
    unittest.main()
