from __future__ import annotations

import json
import os
import shutil
import subprocess
from pathlib import Path

import pytest

pytestmark = pytest.mark.skipif(
    os.environ.get("RUN_EGO_BROWSER_SMOKE") != "1",
    reason="opt-in smoke test (set RUN_EGO_BROWSER_SMOKE=1)",
)

REPO_ROOT = Path(__file__).resolve().parents[2]
FORM_SCRIPT = REPO_ROOT / "scripts" / "ego_browser_form_smoke.sh"
SCREENSHOT_SCRIPT = REPO_ROOT / "scripts" / "ego_browser_screenshot_smoke.sh"


def _extract_last_json(stdout: str) -> dict:
    lines = [line.rstrip() for line in stdout.splitlines() if line.strip()]
    for idx in range(len(lines) - 1, -1, -1):
        if lines[idx].lstrip().startswith("{"):
            candidate = "\n".join(lines[idx:])
            try:
                return json.loads(candidate)
            except json.JSONDecodeError:
                continue
    raise AssertionError(f"No JSON object found in stdout: {stdout!r}")


def _run_script(path: Path, *args: str) -> subprocess.CompletedProcess[str]:
    assert shutil.which("ego-browser"), "ego-browser must be installed for smoke tests"
    assert path.exists(), f"Missing smoke script: {path}"
    env = os.environ.copy()
    env.pop("PYTEST_CURRENT_TEST", None)
    return subprocess.run(
        [str(path), *args],
        cwd=REPO_ROOT,
        env=env,
        capture_output=True,
        text=True,
        timeout=240,
        check=False,
    )


def test_form_smoke_script_round_trips_value() -> None:
    result = _run_script(FORM_SCRIPT, "SmokeValue42")
    assert result.returncode == 0, result.stderr or result.stdout
    payload = _extract_last_json(result.stdout)
    assert payload["title"] == "Hermes Ego Browser Form Smoke"
    assert payload["value"] == "SmokeValue42"
    assert payload["status"] == "value:SmokeValue42"
    assert payload["url"].startswith("http://127.0.0.1:")


def test_screenshot_smoke_script_captures_google_homepage() -> None:
    if os.environ.get("RUN_EGO_BROWSER_SCREENSHOT_SMOKE") != "1":
        pytest.skip("set RUN_EGO_BROWSER_SCREENSHOT_SMOKE=1 for the chat-driven screenshot smoke")
    result = _run_script(SCREENSHOT_SCRIPT)
    assert result.returncode == 0, result.stderr or result.stdout
    payload = _extract_last_json(result.stdout)
    assert payload["title"] == "Google"
    assert payload["url"].startswith("https://www.google.com/")
    assert payload["capture_method"] == "browser_vision-via-hermes-chat"
    assert payload["used_ego_browser_skill"] is True
    assert payload["loaded_generic_screenshot_skill"] is False
    assert "Google" in payload["ocr_text"]
    screenshot_path = Path(payload["screenshot_path"])
    assert screenshot_path.exists(), f"Screenshot missing: {screenshot_path}"
    assert screenshot_path.stat().st_size > 0
