#!/usr/bin/env python3
from __future__ import annotations
import os, socket, sys
from pathlib import Path

LAB_ROOT = Path(os.getenv("CB26_LAB_ROOT", "/home/oem/CyberBrain26-lab")).resolve()
CHECK_PORTS = os.getenv("CB26_CHECK_PORTS", "0") == "1"

ARTIFACTS = [
    ("3A", "cb26-lab-start.sh", "spousteci skript", True),
    ("3A", "cb26-lab-stop.sh", "zastavovaci skript", True),
    ("3A", ".env.lab", "LAB konfigurace", True),
    ("3B", "core/openclaw_worker.py", "OpenClaw MVP", True),
    ("3C", "core/worktree_manager.py", "worktree manager", True),
    ("3C", "core/task_manager.py", "task manager", True),
    ("3C", "workspaces", "worktree adresar", True),
    ("3D", "core/terminal_service.py", "terminal service", True),
    ("3E", "core/openclaw_adapter.py", "OpenClaw adapter", True),
    ("3F", "core/event_bus.py", "Event bus 3F-1", True),
    ("3F", "core/webapp_phase3fg.py", "Webapp patch 3F-2/3", True),
    ("3F", "data-lab/events", "events dir", True),
    ("3G", "core/task_queue.py", "task queue", True),
    ("web", "webapp.py", "FastAPI app", True),
    ("web", "static_ui.html", "UI", True),
    ("doc", "docs/CyberBrain26-plan.md", "plan v3.1", True),
]

def classify(path, expected):
    e = path.exists()
    if expected and e: return "ok"
    if expected and not e: return "missing"
    if not expected and not e: return "expected_missing"
    return "extra"

def main():
    print(f"LAB_ROOT: {LAB_ROOT}")
    if not LAB_ROOT.exists():
        print(f"NEEXISTUJE: {LAB_ROOT}")
        return 2
    if str(LAB_ROOT) == "/home/oem/CyberBrain26":
        print("POZOR: LAB_ROOT je BETA!")
        return 2
    print()
    print("=== Kontrola artefaktu ===")
    counts = {"ok": 0, "missing": 0, "expected_missing": 0, "extra": 0}
    for phase, rel, note, expected in ARTIFACTS:
        pp = LAB_ROOT / rel
        st = classify(pp, expected)
        counts[st] += 1
        icon = {"ok": "[OK]  ", "missing": "[MISS]", "expected_missing": "[WAIT]", "extra": "[INFO]"}[st]
        print(f"{icon} [{phase:3s}] {rel:32s} {st:16s} - {note}")
    print()
    print("=== Souhrn ===")
    print(f"  OK:               {counts['ok']}")
    print(f"  Ocekavane chybi:  {counts['expected_missing']}")
    print(f"  Chybi:            {counts['missing']}")
    print(f"  Navic:            {counts['extra']}")
    return 1 if counts["missing"] > 0 else 0

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