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_v473_htmx_helpers_and_labels_exist():
    assert (ROOT / "app/admin_ui/labels.py").exists()
    assert (ROOT / "app/admin_ui/htmx.py").exists()
    assert (ROOT / "app/admin_ui/view_models/operations.py").exists()
    labels = read("app/admin_ui/labels.py")
    assert "ACTION_LABELS" in labels
    assert '"CONFIRM_PAYMENT": "Confirmar pagamento"' in labels
    assert '"ASSOCIATE_OPPORTUNITY": "Associar oportunidade"' in labels


def test_v473_operations_has_htmx_work_items_partial():
    operations = read("app/admin_ui/pages/operations.py")
    vm = read("app/admin_ui/view_models/operations.py")
    assert '@router.get("/operations/partials/work-items"' in operations
    assert 'id="operations-work-items"' in operations
    assert 'hx-get="{esc(partial_href)}"' in operations
    assert 'hx-target="#operations-work-items"' in operations
    assert 'hx-push-url' in operations
    assert "build_operations_view_model" in vm
    assert "operation_item_matches_scope" in vm


def test_v473_tasks_has_htmx_list_partial():
    tasks = read("app/admin_ui/pages/tasks.py")
    assert '@router.get("/tasks/partials/list"' in tasks
    assert 'id="tasks-list"' in tasks
    assert 'hx-get="/tasks/partials/list' in tasks
    assert 'hx-target="#tasks-list"' in tasks
    assert 'hx-push-url' in tasks


def test_v473_existing_opportunity_partials_remain_available():
    opportunities = read("app/admin_ui/pages/opportunities.py")
    assert '@router.get("/opportunities/{opportunity_id}/partials/jasmin-documents"' in opportunities
    assert '@router.get("/opportunities/{opportunity_id}/partials/products"' in opportunities
    assert 'hx-target="#jasmin-documents-panel"' in read("app/admin_dashboard.py")
    assert 'hx-target="#opportunity-products-panel"' in read("app/admin_dashboard.py")


def test_v473_documentation_states_no_db_or_workflow_change():
    doc = read("docs/CLIENTFLOW_V473_HTMX_PARTIALS.md")
    assert "Não altera regras de negócio" in doc
    assert "Não requer migração" not in doc or "base de dados" in doc
    assert "/operations/partials/work-items" in doc
    assert "/tasks/partials/list" in doc
