#!/usr/bin/env bash
set -euo pipefail

python3 -m compileall -q agentrail tests
python3 - <<'PY'
from __future__ import annotations

import ast
from pathlib import Path

missing: list[str] = []
for path in sorted(Path("agentrail").rglob("*.py")):
    tree = ast.parse(path.read_text(encoding="utf-8"), filename=str(path))
    for node in tree.body:
        if isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef)) and not node.name.startswith("_"):
            for arg in [*node.args.posonlyargs, *node.args.args, *node.args.kwonlyargs]:
                if arg.arg != "self" and arg.annotation is None:
                    missing.append(f"{path}:{node.lineno} {node.name} missing annotation for {arg.arg}")
            if node.returns is None:
                missing.append(f"{path}:{node.lineno} {node.name} missing return annotation")
        if isinstance(node, ast.ClassDef):
            for item in node.body:
                if isinstance(item, (ast.FunctionDef, ast.AsyncFunctionDef)) and not item.name.startswith("_"):
                    for arg in [*item.args.posonlyargs, *item.args.args, *item.args.kwonlyargs]:
                        if arg.arg != "self" and arg.annotation is None:
                            missing.append(f"{path}:{item.lineno} {node.name}.{item.name} missing annotation for {arg.arg}")
                    if item.returns is None:
                        missing.append(f"{path}:{item.lineno} {node.name}.{item.name} missing return annotation")
if missing:
    print("Python typing gate failed:")
    for item in missing:
        print(f"- {item}")
    raise SystemExit(1)
print("Python typing gate passed: compileall plus public function annotations")
PY
