#!/usr/bin/env -S uv run --script
# /// script
# requires-python = ">=3.11,<3.12"
# dependencies = ["pydantic>=2", "pyyaml", "sc-compose>=1.7.0,<2"]
# ///
# pyright: strict
"""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
from typing import TypedDict
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parent))

import yaml
from operation_models import matches_typeddict


class RoleRegistry(TypedDict, total=False):
    roles: dict[str, str]

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")
    document: object = yaml.safe_load(registry.read_text())
    if not matches_typeddict(document, RoleRegistry):
        raise ValueError("registry does not match its schema")
    loaded = document
    roles = loaded.get("roles", {})
except (OSError, subprocess.CalledProcessError, yaml.YAMLError, ValueError) as exc:
    print(f"resolve-role: cannot read the role map: {exc}", file=sys.stderr)
    sys.exit(2)
member = roles.get(role)
if not member:
    print(f"resolve-role: role '{role}' is not mapped in {registry} (roles:)",
          file=sys.stderr)
    sys.exit(2)
print(member)
