#!/usr/bin/env python3
"""Verify vault-browser stack is working end-to-end."""
import subprocess
import sys

def run(cmd):
    result = subprocess.run(cmd, shell=True, capture_output=True, text=True)
    return result.returncode == 0, result.stdout.strip(), result.stderr.strip()

checks = []

# 1. systemd unit active
ok, out, err = run("systemctl is-active vault-browser.service")
checks.append(("systemd unit active", ok, out))

# 2. port 9124 listening
ok, out, err = run("ss -tlnp | grep ':9124 '")
checks.append(("port 9124 listening", ok, out))

# 3. local HTTP responds
ok, out, err = run("curl -s http://127.0.0.1:9124/index.html | head -1")
checks.append(("local HTTP responds", ok, out))

# 4. DNS resolves
ok, out, err = run("dig +short vault.robblake.cloud @8.8.8.8")
checks.append(("DNS resolves", ok and '172' in out, out))

# 5. HTTPS responds
ok, out, err = run("curl -sk https://vault.robblake.cloud/index.html | head -1")
checks.append(("HTTPS responds", ok and '<!DOCTYPE' in out, out))

all_ok = True
for name, ok, detail in checks:
    status = "✓" if ok else "✗"
    print(f"{status} {name}: {detail[:80]}")
    if not ok:
        all_ok = False

if all_ok:
    print("\nAll checks passed. Vault browser is working.")
    sys.exit(0)
else:
    print("\nSome checks failed. See details above.")
    sys.exit(1)
