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_task_detail_has_opportunity_search_fallback_when_unlinked():
    tasks = read("app/admin_ui/pages/tasks.py")
    assert "def _task_opportunity_navigation_html" in tasks
    assert "Ver oportunidade" in tasks
    assert "Procurar oportunidade" in tasks
    assert "Sem oportunidade ligada diretamente a esta tarefa" in tasks
    assert "_safe_search_query(_task_email(task))" in tasks
    assert "_task_opportunity_navigation_html(task, opportunity_id)" in tasks


def test_commercial_terms_default_to_blif_standard_flow():
    opps = read("app/admin_ui/pages/opportunities.py")
    assert 'metadata.get("payment_terms") or "before_shipping"' in opps
    assert 'metadata.get("delivery_terms") or "carrier"' in opps
    assert 'form.get("payment_terms") or "before_shipping"' in opps
    assert 'form.get("delivery_terms") or "carrier"' in opps
    assert '"before_shipping": "Antes do envio"' in opps
    assert '"carrier": "Transportadora"' in opps
