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_jasmin_fiscal_sync_service_is_conservative():
    source = read("app/jasmin_fiscal_sync_service.py")
    assert "apply_jasmin_fiscal_sync" in source
    assert "COALESCE(NULLIF(email,''), NULLIF(:email,''))" in source
    assert "NIF divergente" in source
    assert "jasmin_fiscal_sync" in source


def test_opportunity_page_exposes_jasmin_fiscal_button():
    source = read("app/admin_ui/pages/opportunities.py")
    assert "Completar com dados Jasmin" in source
    assert "Associar e completar com Jasmin" in source
    assert "/jasmin/complete-fiscal" in source


def test_workflow_auditor_reports_jasmin_fiscal_gaps():
    source = read("app/admin_ui/pages/system.py")
    assert "audit_jasmin_fiscal_gaps" in source
    assert "jasmin_fiscal_data_available" in read("app/jasmin_fiscal_sync_service.py")


def test_legacy_proforma_text_normalized_in_templates():
    source = read("app/message_templates.py")
    assert "Enviar orçamento para pagamento (ORC Jasmin)" in source
    assert "Follow-up de orçamento para pagamento" in source
