from pathlib import Path

ROOT = Path(__file__).resolve().parents[1]


def read(path: str) -> str:
    return (ROOT / path).read_text(encoding="utf-8")


def test_opportunity_finance_quick_card_matches_blif_normal_flow():
    opps = read("app/admin_ui/pages/opportunities.py")
    assert "Financeiro rápido" in opps
    assert "orçamento → pagamento → fatura" in opps
    assert "Confirmar pagamento" in opps
    assert "Criar/enviar fatura" in opps
    assert "Pagamento confirmado com base no orçamento" in opps
    assert "finance_quick_card_html" in opps


def test_next_action_uses_quote_payment_invoice_sequence():
    service = read("app/opportunity_next_action_service.py")
    assert "quotation_doc" in service
    assert "invoice_doc" in service
    assert "confirmar pagamento antes de emitir fatura" in service
    assert "Pagamento confirmado com base em" in service
    assert "Criar/enviar fatura" in service
    assert "SEND_PROFORMA" not in service


def test_workflow_guard_allows_invoice_after_payment_without_sale_and_payment_after_quote():
    guard = read("app/workflow_guard.py")
    assert "Confirmação de pagamento só deve ocorrer depois de orçamento/fatura/pedido de pagamento" in guard
    assert "No fluxo normal BLIF, fatura Jasmin exige pagamento confirmado" in guard
    assert "Fatura Jasmin exige venda Odoo criada" not in guard
    assert 'payment_terms != "after_delivery"' in guard
    assert "Pagamento pós-entrega pendente" in guard
