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_operation_does_not_render_duplicate_active_task_card():
    opps = read("app/admin_ui/pages/opportunities.py")
    assert 'Tarefa ativa folded into "O que fazer agora?"' in opps
    assert '<h2 class="cf-section-title mb-2">Tarefa ativa</h2>' not in opps
    assert 'next_task_focus_html = ""' in opps


def test_customer_new_has_explicit_route_before_dynamic_customer_id_route():
    customers = read("app/admin_ui/pages/customers.py")
    assert '@router.get("/customers/new", response_class=HTMLResponse)' in customers
    assert customers.index('@router.get("/customers/new", response_class=HTMLResponse)') < customers.index('@router.get("/customers/{customer_id}", response_class=HTMLResponse)')
    assert 'Novo cliente fiscal' in customers
    assert 'action="/customers/create"' in customers


def test_fiscal_association_search_uses_customers_q_filter_not_invalid_new_detail():
    opps = read("app/admin_ui/pages/opportunities.py")
    assert '/customers?q=' in opps
    assert 'href="/customers/new"' in opps
    assert '/customers?search=' not in opps
