from app.domain.opportunity_flow.evidence import OpportunityEvidence
from app.domain.opportunity_flow.engine import decide_opportunity_next_action
from app.domain.opportunity_flow.profiles import load_company_profile


def _base(**changes):
    data = dict(
        opportunity_id="opp-1",
        has_fiscal_customer=True,
        fiscal_identity_validated=True,
        fiscal_data_complete=True,
        has_quote=True,
        quote_sent=True,
        has_invoice=True,
        invoice_sent=True,
        payment_confirmed=True,
        has_odoo_sale=True,
        odoo_physical_ready=True,
        odoo_physical_validated=False,
        odoo_ready=False,
    )
    data.update(changes)
    return OpportunityEvidence(**data)


def test_assigned_picking_requires_physical_validation_before_shipping():
    decision = decide_opportunity_next_action(_base(), load_company_profile("blif"))
    assert decision.next_action.code == "VALIDATE_PHYSICAL_ORDER"
    assert "assigned" in decision.reason.lower()


def test_physical_validation_unlocks_shipment_task():
    decision = decide_opportunity_next_action(
        _base(odoo_physical_validated=True, odoo_ready=True),
        load_company_profile("blif"),
    )
    assert decision.next_action.code == "SHIP_ORDER"


def test_action_is_materialized_and_catalogued():
    from pathlib import Path
    root = Path(__file__).resolve().parents[1]
    catalog = (root / "app/action_catalog.py").read_text(encoding="utf-8")
    materializer = (root / "app/opportunity_action_task_materializer.py").read_text(encoding="utf-8")
    assert '"VALIDATE_PHYSICAL_ORDER": {' in catalog
    assert 'MATERIALIZED_ACTIONS.add("VALIDATE_PHYSICAL_ORDER")' in materializer
