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_v474_opportunities_board_partial_exists_and_is_htmx_targeted():
    opportunities = read("app/admin_ui/pages/opportunities.py")
    assert '@router.get("/opportunities/partials/board"' in opportunities
    assert 'id="opportunities-board"' in opportunities
    assert 'hx-get="/opportunities/partials/board"' in opportunities
    assert 'hx-target="#opportunities-board"' in opportunities
    assert 'hx-swap="outerHTML"' in opportunities
    assert 'hx-push-url' in opportunities
    for label in ["Novas", "Orçamento enviado", "Pagamento pendente", "Bloqueadas"]:
        assert label in opportunities
    assert "Pró-forma enviada" not in opportunities


def test_v474_task_detail_partial_and_actions_are_htmx_targeted():
    tasks = read("app/admin_ui/pages/tasks.py")
    assert '@router.get("/tasks/{task_id}/partials/detail"' in tasks
    assert 'id="task-detail-panel"' in tasks
    assert 'hx-target="#task-detail-panel"' in tasks
    assert 'hx-swap="outerHTML"' in tasks
    assert 'hx-post="/tasks/{esc(task_id)}/complete-with-note"' in tasks
    assert 'hx-post="/tasks/{esc(task_id)}/skip"' in tasks
    assert 'render_task_detail_partial(task_id, notice="Tarefa concluída.")' in tasks


def test_v474_outbox_table_partial_and_actions_are_htmx_targeted():
    outbox = read("app/admin_ui/pages/outbox.py")
    assert '@router.get("/outbox/partials/table"' in outbox
    assert 'id="outbox-table"' in outbox
    assert 'hx-get="{_outbox_partial_url' in outbox
    assert 'hx-target="#outbox-table"' in outbox
    assert 'hx-swap="outerHTML"' in outbox
    assert 'hx-post="/outbox/{oid}/pending"' in outbox
    assert 'hx-post="/outbox/{oid}/ignored"' in outbox


def test_v474_documentation_states_no_db_or_workflow_change():
    doc = read("docs/CLIENTFLOW_V474_HTMX_COMPLETION.md")
    assert "HTMX Completion" in doc
    assert "/opportunities/partials/board" in doc
    assert "/tasks/{task_id}/partials/detail" in doc
    assert "/outbox/partials/table" in doc
    assert "Não requer migração de base de dados" in doc
    assert "Não altera auto-complete" in doc
