from __future__ import annotations

import unittest

from epione_templates import load_template_data, render_form, resolve_defaults
from epione_templates.data import find_form


class DefaultResolutionTests(unittest.TestCase):
    @classmethod
    def setUpClass(cls) -> None:
        cls.data = load_template_data()
        cls.form = find_form(cls.data, "knee_ia_steroid_us")
        cls.global_defaults = cls.data["global_defaults"]

    def test_encounter_value_overrides_per_form_default(self) -> None:
        values = resolve_defaults(
            self.form,
            self.global_defaults,
            {"target": "Clinician-entered target"},
        )

        self.assertEqual(values["target"], "Clinician-entered target")

    def test_per_form_default_overrides_global_or_blank(self) -> None:
        values = resolve_defaults(self.form, self.global_defaults)

        self.assertEqual(values["target"], "Suprapatellar recess / intra-articular space")

    def test_global_default_applies_when_form_default_missing(self) -> None:
        values = resolve_defaults(self.form, self.global_defaults)

        self.assertEqual(values["skin_prep"], self.global_defaults["skin_prep"])

    def test_no_default_field_stays_blank_without_encounter_value(self) -> None:
        values = resolve_defaults(self.form, self.global_defaults)

        self.assertEqual(values["side"], "")
        self.assertEqual(values["lot_expiry"], "")

    def test_no_default_field_accepts_encounter_value(self) -> None:
        values = resolve_defaults(
            self.form,
            self.global_defaults,
            {"side": "Left", "lot_expiry": "Lot ABC, expires 2027-05"},
        )

        self.assertEqual(values["side"], "Left")
        self.assertEqual(values["lot_expiry"], "Lot ABC, expires 2027-05")

    def test_rendered_note_has_no_placeholders_when_required_inputs_supplied(self) -> None:
        encounter_values = {
            field["key"]: f"entered {field['key']}"
            for field in self.form["fields"]
            if field.get("default_layer") == "No Default"
        }
        note = render_form(self.form, self.global_defaults, encounter_values)

        self.assertNotIn("{{", note)
        self.assertNotIn("}}", note)

    def test_procedure_forms_include_recommended_next_procedure_section(self) -> None:
        fields = {field["key"]: field for field in self.form["fields"]}
        defaults = self.form["defaults"]

        self.assertIn("Recommended next procedure:", self.form["narrative_template"])
        self.assertEqual(fields["next_procedure_date"]["default_layer"], "No Default")
        self.assertEqual(fields["next_procedure_date"]["input_type"], "date")
        self.assertIn("Triamcinolone", defaults["next_procedure_medication_choice"])
        self.assertIn("Superolateral", defaults["next_procedure_technique"])
        self.assertIn("3 months", defaults["next_procedure_pre_procedure_comments"])

    def test_rfa_next_procedure_default_uses_yearly_interval(self) -> None:
        rfa_form = find_form(self.data, "genicular_rfa")
        defaults = rfa_form["defaults"]

        self.assertIn("Recommended next procedure:", rfa_form["narrative_template"])
        self.assertIn("every year", defaults["next_procedure_pre_procedure_comments"])
        self.assertIn("next procedure", defaults["next_procedure_post_procedure_comments"])

    def test_procedure_text_fields_include_fast_fill_assist(self) -> None:
        fields = {field["key"]: field for field in self.form["fields"]}

        self.assertIn("quick_options", fields["clinical_indication"])
        self.assertIn("Persistent function-limiting pain", fields["clinical_indication"]["quick_options"][0])
        self.assertIn("context_sources", fields["clinical_indication"])
        self.assertIn("prior_treatments", fields["clinical_indication"]["context_sources"])
        self.assertIn("context_sources", fields["diagnosis"])
        self.assertIn("pain_generator_impression", fields["diagnosis"]["context_sources"])


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