#!/usr/bin/env python3
"""
Claude Code SessionStart hook — one-line preflight status printed into session context.

Surfaces the session-setup facts that have caused real lost time:
- FORGEJO_ACCESS_TOKEN present? (goern forgejo-MCP needs it BEFORE launch; missing -> idle bot)
- OBSIDIAN_API_KEY present? (vault MCP)
- DB tunnel localhost:15432 reachable? ("tunnel :8000 is gendesign not tradein" / tunnel-down traps)

Wired in `.claude/settings.json` hooks.SessionStart. ALWAYS exits 0 — informational only,
never blocks. Fast (<~0.6s): single non-blocking TCP probe with a short timeout.
"""
from __future__ import annotations

import os
import socket
import sys


def _tcp_ok(host: str, port: int, timeout: float = 0.5) -> bool:
    try:
        with socket.create_connection((host, port), timeout=timeout):
            return True
    except OSError:
        return False


def main() -> int:
    try:
        forgejo = "set" if os.environ.get("FORGEJO_ACCESS_TOKEN") else "MISSING"
        obsidian = "set" if os.environ.get("OBSIDIAN_API_KEY") else "MISSING"
        tunnel = "up" if _tcp_ok("localhost", 15432) else "DOWN"
        print(
            f"[preflight] FORGEJO_ACCESS_TOKEN={forgejo} · OBSIDIAN_API_KEY={obsidian} "
            f"· db-tunnel(:15432)={tunnel}"
        )
    except Exception:
        pass
    return 0


if __name__ == "__main__":
    sys.exit(main())
