#!/usr/bin/env python3
"""Print the ATM member that fills a role in this repository.

The map is `roles:` in .claude/agents/registry.yaml (role -> member name).
The member's startup prompt in .atm.toml says which directive it runs, so a
skill names only the role. Exit 0 with the member on stdout, 2 when the
registry is unreadable or the role is not mapped.
"""
import pathlib
import subprocess
import sys

import yaml

if len(sys.argv) != 2:
    sys.exit("usage: resolve-role <role>")
role = sys.argv[1]
try:
    top = subprocess.run(["git", "rev-parse", "--show-toplevel"], check=True,
                         capture_output=True, text=True).stdout.strip()
    registry = pathlib.Path(top, ".claude/agents/registry.yaml")
    roles = (yaml.safe_load(registry.read_text()) or {}).get("roles") or {}
except (OSError, subprocess.CalledProcessError, yaml.YAMLError) as exc:
    print(f"resolve-role: cannot read the role map: {exc}", file=sys.stderr)
    sys.exit(2)
member = roles.get(role)
if not isinstance(member, str) or not member:
    print(f"resolve-role: role '{role}' is not mapped in {registry} (roles:)",
          file=sys.stderr)
    sys.exit(2)
print(member)
