from __future__ import annotations

from pathlib import Path


REPO_ROOT = Path(__file__).resolve().parents[2]
START_SCRIPT = REPO_ROOT / "scripts" / "start_vue_dashboard.sh"
STOP_SCRIPT = REPO_ROOT / "scripts" / "stop_vue_dashboard.sh"
RESTART_SCRIPT = REPO_ROOT / "scripts" / "restart_vue_dashboard.sh"
LEGACY_RESTART_SCRIPT = REPO_ROOT / "scripts" / "restart_dashboard.sh"
SYSTEM_OPS = REPO_ROOT / "src" / "jarvis_finance" / "services" / "system_ops.py"


def test_start_script_reasserts_real_runtime_after_venv_activation() -> None:
    script = START_SCRIPT.read_text()

    activate_index = script.index('source "$RUNTIME_DIR/venv/bin/activate"')
    export_index = script.index('export JARVIS_FINANCE_RUNTIME_DIR="$RUNTIME_DIR"')
    db_unset_index = script.index('unset JARVIS_FINANCE_DB_PATH')
    backend_index = script.index('uvicorn jarvis_finance.api.main:app')

    assert activate_index < export_index < backend_index
    assert activate_index < db_unset_index < backend_index


def test_stop_script_uses_canonical_runtime_dir_not_activation_override() -> None:
    script = STOP_SCRIPT.read_text()

    assert 'RUNTIME_DIR="${JARVIS_FINANCE_RUNTIME_DIR:-$HOME/jarvis_runtime/finance-system}"' in script
    assert 'LOG_DIR="$RUNTIME_DIR/logs"' in script


def test_start_script_checks_vite_binary_not_only_node_modules_directory() -> None:
    script = START_SCRIPT.read_text()

    assert 'node_modules/.bin/vite' in script
    assert 'Missing Vite dependency' in script
    assert 'npm install' in script


def test_start_script_verifies_backend_frontend_and_tailscale_health() -> None:
    script = START_SCRIPT.read_text()

    assert 'wait_http "http://127.0.0.1:$BACKEND_PORT/api/health" "Backend"' in script
    assert 'wait_http "http://127.0.0.1:$FRONTEND_PORT/" "Frontend"' in script
    assert 'wait_http "http://$TAILSCALE_IP:$FRONTEND_PORT/" "Tailscale frontend"' in script
    assert 'npm run dev -- --host 0.0.0.0 --port "$FRONTEND_PORT"' in script


def test_stop_script_kills_stale_port_listeners_after_pid_files() -> None:
    script = STOP_SCRIPT.read_text()

    assert 'stop_pid_file "FastAPI backend" "$BACKEND_PID_FILE"' in script
    assert 'stop_pid_file "Vue frontend" "$FRONTEND_PID_FILE"' in script
    assert 'stop_port "FastAPI backend" "$BACKEND_PORT"' in script
    assert 'stop_port "Vue frontend" "$FRONTEND_PORT"' in script


def test_restart_script_orchestrates_stop_then_start_with_healthchecks() -> None:
    script = RESTART_SCRIPT.read_text()
    legacy_script = LEGACY_RESTART_SCRIPT.read_text()

    assert 'stop_vue_dashboard.sh' in script
    assert 'start_vue_dashboard.sh' in script
    assert script.index('stop_vue_dashboard.sh') < script.index('start_vue_dashboard.sh')
    assert 'restart_vue_dashboard.sh' in legacy_script


def test_backend_dashboard_button_dispatches_combined_restart_asynchronously() -> None:
    source = SYSTEM_OPS.read_text()

    assert 'Restart requests are served by the backend' in source
    assert 'subprocess.Popen' in source
    assert 'sleep 1; exec' in source
    assert 'start_new_session=True' in source
    assert 'status": "scheduled"' in source
