from app.domain.opportunity_flow.evidence import build_opportunity_evidence
from app.domain.opportunity_flow.profiles import load_company_profile
from app.domain.opportunity_flow.rules import decide_blif_next_action
from app.domain.opportunity_flow.types import ACTION_CLOSE_OPPORTUNITY, ACTION_SEND_INVOICE, PAYMENT_BEFORE_SHIPPING


def _base_snapshot():
    return {
        "links": [
            {"system": "clientflow", "external_type": "payment", "status": "confirmed"},
            {"system": "odoo", "external_type": "sale_order", "status": "linked"},
        ],
        "cards": [
            {
                "key": "odoo_sale_order",
                "status": "sale",
            },
            {
                "key": "odoo_physical_status",
                "status": "done",
                "payload": {"outgoing_pickings": [{"name": "WH/OUT/00325", "state": "done"}]},
            },
        ],
    }


def test_completed_send_invoice_task_counts_as_invoice_sent_evidence():
    evidence = build_opportunity_evidence(
        {"id": "00000000-0000-0000-0000-000000000106", "metadata": {"payment_terms": PAYMENT_BEFORE_SHIPPING}},
        linked_documents=[
            {
                "id": "doc-fa-134",
                "document_kind": "invoice",
                "document_number": "FA.FA2026.134",
                "role": "current",
                "total_amount": 467.40,
                "payload": {},
            }
        ],
        tasks=[
            {
                "id": "task-send-invoice",
                "action_code": "SEND_INVOICE",
                "status": "completed",
                "completed_at": "2026-07-01T15:18:00Z",
                "action": "Enviar fatura",
                "note": "Enviar fatura FA.FA2026.134 ao cliente.",
                "metadata": {"document_number": "FA.FA2026.134"},
            }
        ],
        operation_snapshot=_base_snapshot(),
        linked_customer={"tax_id": "517270455"},
        fiscal_data_complete=True,
    )

    assert evidence.invoice_sent is True
    decision = decide_blif_next_action(evidence, load_company_profile("blif"))
    assert decision.next_action.code == ACTION_CLOSE_OPPORTUNITY


def test_completed_send_invoice_for_other_invoice_does_not_count():
    evidence = build_opportunity_evidence(
        {"id": "00000000-0000-0000-0000-000000000107", "metadata": {"payment_terms": PAYMENT_BEFORE_SHIPPING}},
        linked_documents=[
            {
                "id": "doc-fa-135",
                "document_kind": "invoice",
                "document_number": "FA.FA2026.135",
                "role": "current",
                "total_amount": 258.30,
                "payload": {},
            }
        ],
        tasks=[
            {
                "id": "task-send-invoice-old",
                "action_code": "SEND_INVOICE",
                "status": "completed",
                "completed_at": "2026-07-01T15:18:00Z",
                "action": "Enviar fatura",
                "note": "Enviar fatura FA.FA2026.134 ao cliente.",
                "metadata": {"document_number": "FA.FA2026.134"},
            }
        ],
        operation_snapshot=_base_snapshot(),
        linked_customer={"tax_id": "500000000"},
        fiscal_data_complete=True,
    )

    assert evidence.invoice_sent is False
    decision = decide_blif_next_action(evidence, load_company_profile("blif"))
    assert decision.next_action.code == ACTION_SEND_INVOICE
