from app.domain.opportunity_flow.evidence import OpportunityEvidence
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_FOLLOW_UP_PAYMENT,
    ACTION_SEND_INVOICE,
    PAYMENT_AFTER_DELIVERY,
    PAYMENT_BEFORE_SHIPPING,
)


def _profile():
    return load_company_profile("blif")


def test_before_shipping_done_payment_invoice_but_not_sent_sends_invoice_first():
    e = OpportunityEvidence(
        opportunity_id="00000000-0000-0000-0000-000000000001",
        has_fiscal_customer=True,
        fiscal_data_complete=True,
        has_quote=True,
        has_invoice=True,
        invoice_number="FA.FA2026.131",
        invoice_sent=False,
        payment_confirmed=True,
        payment_terms=PAYMENT_BEFORE_SHIPPING,
        has_odoo_sale=True,
        order_shipped=True,
    )
    decision = decide_blif_next_action(e, _profile())
    assert decision.next_action.code == ACTION_SEND_INVOICE


def test_before_shipping_done_payment_invoice_sent_closes():
    e = OpportunityEvidence(
        opportunity_id="00000000-0000-0000-0000-000000000002",
        has_fiscal_customer=True,
        fiscal_data_complete=True,
        has_quote=True,
        has_invoice=True,
        invoice_number="FA.FA2026.131",
        invoice_sent=True,
        payment_confirmed=True,
        payment_terms=PAYMENT_BEFORE_SHIPPING,
        has_odoo_sale=True,
        order_shipped=True,
    )
    decision = decide_blif_next_action(e, _profile())
    assert decision.next_action.code == ACTION_CLOSE_OPPORTUNITY


def test_after_delivery_done_invoice_sent_without_payment_follows_payment():
    e = OpportunityEvidence(
        opportunity_id="00000000-0000-0000-0000-000000000003",
        has_fiscal_customer=True,
        fiscal_data_complete=True,
        has_quote=True,
        has_invoice=True,
        invoice_number="FA.FA2026.138",
        invoice_sent=True,
        payment_confirmed=False,
        payment_terms=PAYMENT_AFTER_DELIVERY,
        has_odoo_sale=True,
        order_shipped=True,
    )
    decision = decide_blif_next_action(e, _profile())
    assert decision.next_action.code == ACTION_FOLLOW_UP_PAYMENT


def test_after_delivery_done_invoice_sent_payment_confirmed_closes():
    e = OpportunityEvidence(
        opportunity_id="00000000-0000-0000-0000-000000000004",
        has_fiscal_customer=True,
        fiscal_data_complete=True,
        has_quote=True,
        has_invoice=True,
        invoice_number="FA.FA2026.138",
        invoice_sent=True,
        payment_confirmed=True,
        payment_terms=PAYMENT_AFTER_DELIVERY,
        has_odoo_sale=True,
        order_shipped=True,
    )
    decision = decide_blif_next_action(e, _profile())
    assert decision.next_action.code == ACTION_CLOSE_OPPORTUNITY


def test_static_wh_mo_is_technical_detail_not_close_signal():
    evidence_text = open("app/domain/opportunity_flow/evidence.py", encoding="utf-8").read()
    assert "Ordens de fabrico/MO são detalhe técnico" in evidence_text
    assert "shipment_status in {\"created\", \"sent\", \"shipped\"}" not in evidence_text

    materializer_text = open("app/opportunity_action_task_materializer.py", encoding="utf-8").read()
    assert 'MATERIALIZED_ACTIONS = {"SEND_INVOICE", "FOLLOW_UP_PAYMENT"}' in materializer_text

    guard_text = open("app/workflow_guard.py", encoding="utf-8").read()
    assert "Concluir oportunidade exige evidência de fatura enviada ao cliente" in guard_text
