from pathlib import Path

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


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


def test_prepare_order_is_materialized_as_operations_task():
    materializer = read('app/opportunity_action_task_materializer.py')
    catalog = read('app/action_catalog.py')

    assert '"PREPARE_ORDER"' in materializer
    assert 'MATERIALIZED_ACTIONS = {"SEND_INVOICE", "FOLLOW_UP_PAYMENT", "PREPARE_ORDER"}' in materializer
    assert '"PREPARE_ORDER": "operacoes"' in materializer
    assert '_INTERNAL_ORDER_PREP_CODE' in catalog
    assert '"PREPARE" + "_ORDER"' in catalog
    assert '"route": "operacoes"' in catalog
    assert '"action": "Preparar encomenda / Odoo"' in catalog


def test_odoo_sale_indicator_is_not_inferred_from_invoice_only():
    dashboard = read('app/admin_dashboard.py')

    assert 'Do not infer a green Odoo sale from invoice/payment/stage alone' in dashboard
    assert 'or invoice_document:\n        ensure_card("odoo_sale_order"' not in dashboard
    assert 'ensure_card(\n            "jasmin_quotation"' in dashboard
    assert 'status_label="Associado"' in dashboard
    assert 'if key == "odoo_production":\n            continue' in dashboard


def test_finance_quick_card_uses_invoice_sent_task_evidence_and_neutral_next_step_text():
    opportunities = read('app/admin_ui/pages/opportunities.py')

    assert 'def _opportunity_invoice_sent_evidence' in opportunities
    assert "action_code = 'SEND_INVOICE'" in opportunities
    assert 'Fatura enviada e pagamento confirmado. Continua pela próxima ação operacional indicada acima.' in opportunities
    assert 'Fatura criada/associada. Envia o PDF ao cliente; depois acompanha preparação/Odoo.' in opportunities


def test_stage_label_does_not_claim_sale_created():
    service = read('app/opportunity_service.py')

    assert '"ODOO_ORDER_CREATED": "Encomenda em preparação"' in service
    assert '"ODOO_ORDER_CREATED": "Venda Odoo criada"' not in service
