# -*- coding: utf-8 -*-
"""STEP 1 tests — case lifecycle state machine + attorney-only gates.

Run just this step:
    odoo -d <db> -i activeblue_familylaw --test-enable --test-tags familylaw_step1 --stop-after-init

The tests prove the verifiable behaviour for Step 1:
  * a new case starts in 'intake' with no conflict clearance;
  * a matter cannot be engaged before an attorney clears the conflict;
  * only an attorney can clear the conflict / close the case;
  * illegal (skipping) transitions are rejected;
  * the full forward path works end to end.
"""

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


@tagged("post_install", "-at_install", "familylaw", "familylaw_step1")
class TestCaseLifecycle(TransactionCase):

    @classmethod
    def setUpClass(cls):
        super().setUpClass()
        cls.partner = cls.env["res.partner"].create({"name": "Client A"})

        # An attorney and a paralegal, each in the right group.
        cls.attorney = new_test_user(
            cls.env,
            login="fl_attorney",
            name="Test Attorney",
            email="attorney@example.com",
            groups="base.group_user,activeblue_familylaw_v2.group_familylaw_attorney",
        )
        cls.paralegal = new_test_user(
            cls.env,
            login="fl_paralegal",
            name="Test Paralegal",
            email="paralegal@example.com",
            groups="base.group_user,activeblue_familylaw_v2.group_familylaw_user",
        )

        cls.Case = cls.env["familylaw.case"]
        cls.case = cls.Case.create({
            "name": "In re the Marriage of Client A",
            "client_id": cls.partner.id,
            "case_type": "support_modification",
            "attorney_id": cls.attorney.id,
            "paralegal_id": cls.paralegal.id,
        })

    # --- defaults -----------------------------------------------------------
    def test_01_initial_state(self):
        self.assertEqual(self.case.state, "intake")
        self.assertFalse(self.case.conflict_check_cleared)
        self.assertEqual(self.case.representation, "attorney")

    # --- conflict-clearance gate -------------------------------------------
    def test_02_engage_blocked_without_clearance(self):
        with self.assertRaises(UserError):
            self.case.action_engage()
        self.assertEqual(self.case.state, "intake")

    def test_03_clearance_requires_attorney(self):
        with self.assertRaises(UserError):
            self.case.with_user(self.paralegal).action_mark_conflict_cleared()
        self.assertFalse(self.case.conflict_check_cleared)

    def test_04_attorney_clears_conflict(self):
        self.case.with_user(self.attorney).action_mark_conflict_cleared()
        self.assertTrue(self.case.conflict_check_cleared)

    def test_05_engage_after_clearance(self):
        self.case.with_user(self.attorney).action_mark_conflict_cleared()
        self.case.action_engage()
        self.assertEqual(self.case.state, "engaged")

    # --- transition guards --------------------------------------------------
    def test_06_illegal_transition_blocked(self):
        # cannot jump straight from intake to discovery
        with self.assertRaises(UserError):
            self.case.action_start_discovery()
        self.assertEqual(self.case.state, "intake")

    def test_07_full_forward_path(self):
        self.case.with_user(self.attorney).action_mark_conflict_cleared()
        self.case.action_engage()
        self.assertEqual(self.case.state, "engaged")
        self.case.action_start_disclosure()
        self.assertEqual(self.case.state, "disclosure")
        self.case.action_start_discovery()
        self.assertEqual(self.case.state, "discovery")
        self.case.action_start_mediation()
        self.assertEqual(self.case.state, "mediation")
        self.case.action_set_hearing()
        self.assertEqual(self.case.state, "hearing")
        self.case.with_user(self.attorney).action_close()
        self.assertEqual(self.case.state, "closed")

    # --- closing / reopening gate ------------------------------------------
    def test_08_close_requires_attorney(self):
        with self.assertRaises(UserError):
            self.case.with_user(self.paralegal).action_close()
        self.assertNotEqual(self.case.state, "closed")

    def test_09_reopen_requires_attorney_and_closed_state(self):
        # reopen only valid from 'closed'
        with self.assertRaises(UserError):
            self.case.action_reopen()
        # close then reopen as attorney
        self.case.with_user(self.attorney).action_mark_conflict_cleared()
        self.case.with_user(self.attorney).action_close()
        self.assertEqual(self.case.state, "closed")
        self.case.with_user(self.attorney).action_reopen()
        self.assertEqual(self.case.state, "engaged")

    # --- audit trail (mail.thread tracking) --------------------------------
    def test_10_state_change_is_logged(self):
        before = len(self.case.message_ids)
        self.case.with_user(self.attorney).action_mark_conflict_cleared()
        self.case.action_engage()
        self.assertGreater(
            len(self.case.message_ids), before,
            "Stage changes and conflict clearance should post to the chatter "
            "(the audit trail).",
        )
