#!/usr/bin/env python3
"""CLI entry point for lantern-games."""
import argparse
import sys

from lantern_games.games import GAME_REGISTRY


def main():
    parser = argparse.ArgumentParser(
        prog="lantern",
        description="Terminal games for the idle moments between coding agent runs.",
    )
    subparsers = parser.add_subparsers(dest="command")

    # "play" subcommand
    play_parser = subparsers.add_parser("play", help="Play a game")
    play_parser.add_argument(
        "game", choices=GAME_REGISTRY.keys(), help="Name of the game"
    )

    # Direct game names as top-level subcommands
    for name, info in GAME_REGISTRY.items():
        subparsers.add_parser(name, help=info["description"])

    # "list" subcommand
    subparsers.add_parser("list", help="List available games")

    args = parser.parse_args()

    if args.command is None:
        parser.print_help()
        sys.exit(1)

    if args.command == "list":
        print("\nAvailable games:\n")
        for name, info in GAME_REGISTRY.items():
            print(f"  {name:20s} {info['description']}")
        print(f"\nRun: lantern <game-name>\n")
        return

    game_name = args.game if args.command == "play" else args.command
    GAME_REGISTRY[game_name]["entry"]()
