from __future__ import annotations

import tempfile
import unittest
from pathlib import Path

import openpyxl

from epione_templates import load_template_data
from epione_templates.artifacts import (
    PHYSICIAN_REVIEW_DASHBOARD_HEADERS,
    PRODUCT_LIBRARY_OUTPUT_HEADERS,
    physician_review_dashboard_rows,
    write_v07_outputs,
)


class ArtifactTests(unittest.TestCase):
    def test_v07_workbook_outputs_include_verification_columns(self) -> None:
        root = Path(__file__).resolve().parents[1]
        data = load_template_data()

        with tempfile.TemporaryDirectory() as temp_dir:
            outputs = write_v07_outputs(root, Path(temp_dir), data)
            workbook = openpyxl.load_workbook(outputs["local_workbook"], read_only=True)
            worksheet = workbook["Product_Library"]
            headers = [cell.value for cell in worksheet[3]]

        self.assertEqual(headers, PRODUCT_LIBRARY_OUTPUT_HEADERS)
        self.assertIn("verified_by", headers)
        self.assertIn("DIN_or_licence_number", headers)
        self.assertIn("approved_for_default_yes_no", headers)

    def test_workbook_outputs_include_fast_fill_columns(self) -> None:
        root = Path(__file__).resolve().parents[1]
        data = load_template_data()

        with tempfile.TemporaryDirectory() as temp_dir:
            outputs = write_v07_outputs(root, Path(temp_dir), data)
            workbook = openpyxl.load_workbook(outputs["local_workbook"], read_only=True)
            registry = workbook["Field_Registry"]
            procedure = workbook["Knee_Steroid_US"]
            registry_headers = [cell.value for cell in registry[3]]
            procedure_headers = [cell.value for cell in procedure[8]]
            clinical_row = [
                row
                for row in procedure.iter_rows(values_only=True)
                if row and row[2] == "clinical_indication"
            ][0]

        self.assertIn("quick_options", registry_headers)
        self.assertIn("context_sources", registry_headers)
        self.assertIn("Quick text templates", procedure_headers)
        self.assertIn("History/exam context sources", procedure_headers)
        self.assertIn("Persistent function-limiting pain", clinical_row[11])
        self.assertIn("prior_treatments", clinical_row[12])

    def test_v07_workbook_outputs_include_next_procedure_fields(self) -> None:
        root = Path(__file__).resolve().parents[1]
        data = load_template_data()

        with tempfile.TemporaryDirectory() as temp_dir:
            outputs = write_v07_outputs(root, Path(temp_dir), data)
            workbook = openpyxl.load_workbook(outputs["local_workbook"], read_only=True)
            procedure_sheet = workbook["Knee_Steroid_US"]
            template_sheet = workbook["Form_Templates"]
            default_sheet = workbook["Per_Form_Defaults"]
            procedure_values = [
                row
                for row in procedure_sheet.iter_rows(values_only=True)
                if row and row[2] == "next_procedure_date"
            ]
            template_values = [
                row[4]
                for row in template_sheet.iter_rows(values_only=True)
                if row and row[0] == "knee_ia_steroid_us"
            ]
            default_keys = {
                row[0]
                for row in default_sheet.iter_rows(values_only=True)
                if row and row[0]
            }

        self.assertEqual(len(procedure_values), 1)
        self.assertIn("Recommended next procedure:", template_values[0])
        self.assertIn("knee_ia_steroid_us|next_procedure_medication_choice", default_keys)

    def test_html_app_includes_fast_fill_controls(self) -> None:
        root = Path(__file__).resolve().parents[1]
        data = load_template_data()

        with tempfile.TemporaryDirectory() as temp_dir:
            outputs = write_v07_outputs(root, Path(temp_dir), data)
            html = Path(outputs["html_app"]).read_text(encoding="utf-8")

        self.assertIn("History / Physical Exam Assist", html)
        self.assertIn("quick-choice-list", html)
        self.assertIn("data-preset-text", html)
        self.assertIn("Pull Hx/Exam Into Procedure", html)
        self.assertIn("context_sources", html)

    def test_physician_review_dashboard_has_one_row_per_form(self) -> None:
        data = load_template_data()
        rows = physician_review_dashboard_rows(data)

        self.assertEqual(len(rows), len(data["forms"]))
        self.assertEqual(set(PHYSICIAN_REVIEW_DASHBOARD_HEADERS), set(rows[0]))
        self.assertEqual(rows[0]["review_status"], "Needs physician review")
        self.assertGreater(rows[0]["required_fields"], 0)
        self.assertGreater(rows[0]["manual_required_fields"], 0)


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