# -*- coding: utf-8 -*-
"""STEP 10 tests — emergency workflow (12.941 pick-up/removal).

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

Proves:
  * the have-order-vs-not fork seeds different required attachments;
  * a missing required attachment BLOCKS marking ready / filing;
  * once complete, the motion can be marked ready and filed;
  * the intake fast-path opens a matter on minimum facts (is_emergency) and an
    emergency motion can be created on it.
"""

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


@tagged("post_install", "-at_install", "familylaw", "familylaw_step10")
class TestStep10Emergency(TransactionCase):

    @classmethod
    def setUpClass(cls):
        super().setUpClass()
        cls.partner = cls.env["res.partner"].create({"name": "Emerg Client"})
        cls.case = cls.env["familylaw.case"].create({
            "name": "Emergency Matter", "client_id": cls.partner.id,
            "case_type": "enforcement", "is_emergency": True,
        })
        cls.Motion = cls.env["familylaw.emergency.motion"]

    # --- the fork -----------------------------------------------------------
    def test_01_no_order_fork_requirements(self):
        m = self.Motion.open_for_case(self.case, "pickup_order", False)
        names = m.attachment_ids.mapped("name")
        self.assertTrue(any("sworn affidavit" in n.lower() or "verified motion" in n.lower()
                            for n in names))

    def test_02_have_order_fork_requirements(self):
        m = self.Motion.open_for_case(self.case, "pickup_order", True)
        names = m.attachment_ids.mapped("name")
        self.assertTrue(any("certified copy" in n.lower() for n in names))

    def test_03_forks_differ(self):
        m_no = self.Motion.open_for_case(self.case, "pickup_order", False)
        m_yes = self.Motion.open_for_case(self.case, "pickup_order", True)
        self.assertNotEqual(
            set(m_no.attachment_ids.mapped("name")),
            set(m_yes.attachment_ids.mapped("name")),
        )

    # --- missing-attachment block -------------------------------------------
    def test_04_missing_blocks_ready(self):
        m = self.Motion.open_for_case(self.case, "pickup_order", True)
        # nothing provided yet
        self.assertGreater(m.missing_required_count, 0)
        with self.assertRaises(UserError):
            m.action_mark_ready()
        self.assertEqual(m.state, "draft")

    def test_05_partial_still_blocks(self):
        m = self.Motion.open_for_case(self.case, "pickup_order", True)
        m.attachment_ids[0].provided = True
        if len(m.attachment_ids) > 1:
            with self.assertRaises(UserError):
                m.action_mark_ready()

    def test_06_complete_allows_ready(self):
        m = self.Motion.open_for_case(self.case, "pickup_order", True)
        m.attachment_ids.write({"provided": True})
        m.action_mark_ready()
        self.assertEqual(m.state, "ready")
        self.assertEqual(m.missing_required_count, 0)

    def test_07_file_requires_ready(self):
        m = self.Motion.open_for_case(self.case, "pickup_order", True)
        with self.assertRaises(UserError):
            m.action_file()
        m.attachment_ids.write({"provided": True})
        m.action_mark_ready()
        m.action_file()
        self.assertEqual(m.state, "filed")

    def test_08_no_checklist_blocks(self):
        m = self.Motion.create({
            "proceeding_id": self.case.proceeding_ids[0].id,
            "motion_type": "pickup_order",
        })
        # remove any seeded items
        m.attachment_ids.unlink()
        with self.assertRaises(UserError):
            m.action_mark_ready()

    # --- fast-path intake ---------------------------------------------------
    def test_09_intake_fast_path_then_motion(self):
        wiz = self.env["familylaw.intake.wizard"].create({
            "caller_name": "Panicked Parent",
            "urgency_child_withheld": True,
        })
        self.assertTrue(wiz.is_emergency)
        wiz._create_emergency_case()
        case = wiz.created_case_id
        self.assertTrue(case.is_emergency)
        # emergency motion on the fast-path matter
        m = self.Motion.open_for_case(case, "pickup_order", False)
        self.assertEqual(m.case_id, case)
        self.assertTrue(m.attachment_ids)

    def test_10_motion_uses_open_proceeding(self):
        m = self.Motion.open_for_case(self.case, "prevent_removal", False)
        self.assertEqual(m.proceeding_id.state, "open")
        self.assertEqual(m.case_id, self.case)
