#!/usr/bin/env python3
import os
import pathlib
import sys

root_dir = os.environ.get("ROOT_DIR")
if not root_dir:
    print("ERROR:missing_env:ROOT_DIR environment variable not set")
    sys.exit(2)

root = pathlib.Path(root_dir)
reg = root / ".smbatcher" / "REGISTRY.md"

if not reg.exists():
    print("ERROR:registry_not_found:" + str(reg))
    sys.exit(2)

batches = []
for line in reg.read_text().splitlines():
    if line.startswith("|") and "Batch" not in line and "--------" not in line and "|" in line:
        cols = [c.strip() for c in line.strip("|").split("|")]
        if len(cols) >= 5 and cols[4] not in ("-", ""):
            batches.append(cols[4])
batch_id = sorted(set(batches))[-1] if batches else "001"
print(f"OK:batch_id:{batch_id}")
