"""Smoke tests for the unified Echo dashboard (post index+eco merge)."""
import json
from pathlib import Path

import pytest
import requests

BASE = 'http://localhost:8088'
DASH = Path(__file__).resolve().parent.parent / 'dashboard'

SIBLING_PAGES = ['index', 'habits', 'files', 'workspace', 'notes', 'grup-sprijin']


def _taskboard_running():
    try:
        return requests.get(f'{BASE}/api/status', timeout=2).ok
    except Exception:
        return False


pytestmark = pytest.mark.skipif(
    not _taskboard_running(),
    reason='echo-taskboard not reachable on :8088 — start it with `systemctl --user start echo-taskboard`',
)


@pytest.mark.parametrize('page', SIBLING_PAGES)
def test_no_eco_link_on_sibling_pages(page):
    """No sibling page should still have a clickable link to the removed eco.html."""
    r = requests.get(f'{BASE}/{page}.html', timeout=5)
    assert r.status_code == 200, f'{page}.html returned {r.status_code}'
    # Only reject actual hyperlinks, not historical code comments.
    assert 'href="/echo/eco.html"' not in r.text, f'{page}.html still links to eco.html'
    assert "href='/echo/eco.html'" not in r.text


def test_index_has_all_panels():
    """Unified index must render Git, Services, Sessions, Logs, Doctor panels."""
    r = requests.get(f'{BASE}/index.html', timeout=5)
    assert r.status_code == 200
    for sid in ['sec-git', 'sec-services', 'sec-sessions', 'sec-logs', 'sec-doctor']:
        assert f'id="{sid}"' in r.text, f'missing panel #{sid}'


def test_swipe_nav_no_eco():
    """swipe-nav.js hardcoded page list must no longer include eco.html."""
    js = (DASH / 'swipe-nav.js').read_text(encoding='utf-8')
    assert "'eco.html'" not in js


def test_eco_html_gone():
    """The old eco.html route should now 404."""
    r = requests.get(f'{BASE}/eco.html', timeout=5)
    assert r.status_code == 404


def test_api_git_has_diffstat_field():
    """Regression guard: the Git panel depends on /api/git returning diffStat."""
    r = requests.get(f'{BASE}/api/git', timeout=5)
    assert r.status_code == 200
    data = r.json()
    assert 'diffStat' in data
    assert 'uncommittedParsed' in data
    assert 'lastCommit' in data


def test_api_eco_git_removed():
    """The orphaned /api/eco/git GET handler was deleted during unification."""
    r = requests.get(f'{BASE}/api/eco/git', timeout=5)
    assert r.status_code == 404


def test_nav_injected_server_side():
    """Server must replace <!--NAV--> with real nav markup on every .html."""
    r = requests.get(f'{BASE}/index.html', timeout=5)
    assert r.status_code == 200
    assert '<!--NAV-->' not in r.text, 'server did not expand the NAV marker'
    assert 'class="nav-item"' in r.text, 'nav did not render'
    assert 'data-page="index"' in r.text


@pytest.mark.parametrize('page', SIBLING_PAGES)
def test_nav_present_on_all_pages(page):
    """Every sibling page must receive the injected nav."""
    r = requests.get(f'{BASE}/{page}.html', timeout=5)
    assert r.status_code == 200
    assert 'class="nav-item"' in r.text
    assert '<!--NAV-->' not in r.text
