# -*- coding: utf-8 -*-
"""STEP 13 tests — Miami-Dade auto-seed (AO 14-13).

    odoo -d <db> -u activeblue_familylaw --test-enable \
         --test-tags familylaw_step13 --stop-after-init

Proves:
  * obligations seed on case open for Miami-Dade matters (by case type);
  * non-Miami-Dade matters do not seed;
  * the course-before-judgment guard blocks setting the final hearing while the
    parenting course is pending, and allows it once completed;
  * seeding is idempotent.
"""

from odoo.tests.common import TransactionCase, new_test_user, tagged
from odoo.exceptions import UserError


@tagged("post_install", "-at_install", "familylaw", "familylaw_step13")
class TestStep13Obligations(TransactionCase):

    @classmethod
    def setUpClass(cls):
        super().setUpClass()
        cls.attorney = new_test_user(
            cls.env, login="fl_atty13", name="Attorney 13",
            email="atty13@example.com",
            groups="base.group_user,activeblue_familylaw_v2.group_familylaw_attorney",
        )
        cls.partner = cls.env["res.partner"].create({"name": "AO Client"})
        cls.Case = cls.env["familylaw.case"]

    def _case(self, **kw):
        vals = {"name": "AO Matter", "client_id": self.partner.id,
                "case_type": "dissolution_children", "county": "miami_dade"}
        vals.update(kw)
        return self.Case.create(vals)

    # --- seeding ------------------------------------------------------------
    def test_01_seed_dissolution_children(self):
        case = self._case(case_type="dissolution_children")
        types = set(case.obligation_ids.mapped("obligation_type"))
        self.assertEqual(types, {"status_quo_order", "parenting_course", "mediation"})

    def test_02_seed_dissolution_no_children(self):
        case = self._case(case_type="dissolution_no_children")
        types = set(case.obligation_ids.mapped("obligation_type"))
        self.assertIn("status_quo_order", types)
        self.assertIn("mediation", types)
        self.assertNotIn("parenting_course", types)

    def test_03_seed_paternity_has_course(self):
        case = self._case(case_type="paternity")
        types = set(case.obligation_ids.mapped("obligation_type"))
        self.assertIn("parenting_course", types)

    def test_04_non_miamidade_no_seed(self):
        case = self._case(county="broward")
        self.assertFalse(case.obligation_ids)

    def test_05_seed_idempotent(self):
        case = self._case()
        n1 = len(case.obligation_ids)
        case.action_seed_obligations()
        self.assertEqual(len(case.obligation_ids), n1)

    # --- course-before-judgment guard ---------------------------------------
    def test_06_guard_blocks_hearing_with_pending_course(self):
        case = self._case(case_type="dissolution_children")
        with self.assertRaises(UserError):
            case._check_parenting_course_before_judgment()

    def test_07_guard_passes_after_course_complete(self):
        case = self._case(case_type="dissolution_children")
        course = case.obligation_ids.filtered(
            lambda o: o.obligation_type == "parenting_course")
        course.action_complete()
        # should not raise
        case._check_parenting_course_before_judgment()

    def test_08_guard_in_full_path(self):
        case = self._case(case_type="dissolution_children")
        case.with_user(self.attorney).action_mark_conflict_cleared()
        case.action_engage()
        case.action_start_disclosure()
        case.action_start_discovery()
        case.action_start_mediation()
        # parenting course still pending -> set hearing blocked
        with self.assertRaises(UserError):
            case.action_set_hearing()
        # complete course -> allowed
        case.obligation_ids.filtered(
            lambda o: o.obligation_type == "parenting_course").action_complete()
        case.action_set_hearing()
        self.assertEqual(case.state, "hearing")

    def test_09_no_children_not_blocked(self):
        case = self._case(case_type="dissolution_no_children")
        # no parenting_course obligation -> guard passes
        case._check_parenting_course_before_judgment()

    def test_10_obligation_complete_waive(self):
        case = self._case()
        ob = case.obligation_ids[0]
        ob.action_complete()
        self.assertEqual(ob.state, "completed")
        self.assertTrue(ob.completed_date)
