# -*- coding: utf-8 -*-
"""
Tests for l10n_ru_attorney — attorney (base.consent) creation, autonumbering,
and linkage to purchase orders.

Validates: Requirements 17.1, 17.2, 17.3
"""
from odoo.tests.common import TransactionCase


# ---------------------------------------------------------------------------
# Helpers
# ---------------------------------------------------------------------------

def _make_partner(env):
    """Create a company partner (supplier)."""
    return env['res.partner'].create({
        'name': 'Test Supplier Attorney',
        'is_company': True,
    })


def _make_employee(env):
    """Create a minimal hr.employee."""
    return env['hr.employee'].create({
        'name': 'Test Employee Attorney',
    })


def _make_purchase_order(env, partner):
    """Create a minimal purchase.order."""
    return env['purchase.order'].create({
        'partner_id': partner.id,
    })


def _make_consent(env, partner, employee, purchase_order, name=None):
    """Create a base.consent record."""
    vals = {
        'partner_id': partner.id,
        'employee_id': employee.id,
        'purchaseorder_id': purchase_order.id,
    }
    if name:
        vals['name'] = name
    return env['base.consent'].create(vals)


# ---------------------------------------------------------------------------
# TestBaseConsent
# ---------------------------------------------------------------------------

class TestBaseConsent(TransactionCase):
    """
    Tests for base.consent creation: autonumbering via ir.sequence,
    write-back of consent_id to purchase.order, and fill_order constraint.

    Validates: Requirements 17.1, 17.2, 17.3
    """

    def setUp(self):
        super().setUp()
        self.partner = _make_partner(self.env)
        self.employee = _make_employee(self.env)
        self.purchase_order = _make_purchase_order(self.env, self.partner)

    # ------------------------------------------------------------------
    # Requirement 17.1 — autonumber via ir.sequence when name is absent
    # ------------------------------------------------------------------

    def test_create_autonumber(self):
        """
        Req 17.1 — creating base.consent without a name assigns a number
        via ir.sequence with code 'base.consent'.
        """
        consent = _make_consent(
            self.env, self.partner, self.employee, self.purchase_order
        )
        self.assertTrue(
            consent.name,
            "consent.name should be set automatically via ir.sequence",
        )
        # A manually-supplied name must be preserved as-is
        consent2 = _make_consent(
            self.env, self.partner, self.employee, self.purchase_order,
            name='MANUAL-001',
        )
        self.assertEqual(
            consent2.name, 'MANUAL-001',
            "Explicitly supplied name should not be overwritten",
        )

    # ------------------------------------------------------------------
    # Requirement 17.2 — purchase.order.consent_id set after create
    # ------------------------------------------------------------------

    def test_create_writes_back_to_purchase_order(self):
        """
        Req 17.2 — after creating base.consent linked to a purchase.order,
        purchase.order.consent_id equals the created consent.
        """
        consent = _make_consent(
            self.env, self.partner, self.employee, self.purchase_order
        )
        self.purchase_order.invalidate_recordset()
        self.assertEqual(
            self.purchase_order.consent_id.id,
            consent.id,
            "purchase.order.consent_id should be set to the created consent",
        )

    # ------------------------------------------------------------------
    # Requirement 17.3 — fill_order constraint writes consent_id to order
    # ------------------------------------------------------------------

    def test_constrains_fill_order(self):
        """
        Req 17.3 — the @api.constrains('purchaseorder_id') method fill_order
        writes consent_id into the linked purchase.order.
        """
        # Create a second purchase order to reassign to
        second_order = _make_purchase_order(self.env, self.partner)

        consent = _make_consent(
            self.env, self.partner, self.employee, self.purchase_order
        )

        # Reassign to second_order — this triggers fill_order constraint
        consent.write({'purchaseorder_id': second_order.id})

        second_order.invalidate_recordset()
        self.assertEqual(
            second_order.consent_id.id,
            consent.id,
            "fill_order should write consent_id into the newly assigned purchase.order",
        )
