#!/usr/bin/env python3 """ Major Domo CLI - Game Results Operations Commands for listing and filtering game results by team, week, and season. """ from typing import Annotated, Optional import typer from cli_common import ( state, console, output_json, output_table, handle_error, get_season, safe_nested, ) results_app = typer.Typer( help="Game results operations", invoke_without_command=True, no_args_is_help=False, ) @results_app.callback() def results_callback( ctx: typer.Context, team: Annotated[ Optional[str], typer.Option("--team", "-t", help="Filter by team abbreviation") ] = None, week: Annotated[ Optional[int], typer.Option("--week", "-w", help="Filter by specific week") ] = None, week_start: Annotated[ Optional[int], typer.Option("--week-start", help="Start week for range") ] = None, week_end: Annotated[ Optional[int], typer.Option("--week-end", help="End week for range") ] = None, season: Annotated[ Optional[int], typer.Option("--season", "-s", help="Season number") ] = None, limit: Annotated[ int, typer.Option("--limit", "-n", help="Max results to display") ] = 50, ): """ List game results with optional filters. By default, shows recent game results for the current season. Use filters to narrow down results. Examples: majordomo results --team CAR majordomo results --week 5 majordomo results --week-start 1 --week-end 4 """ # Only invoke if no subcommand was called if ctx.invoked_subcommand is not None: return try: season = get_season(season) # Handle week vs week_start/week_end if week is not None: week_start = week week_end = week # Use low-level API to access all parameters result = state.api.get( "results", season=season, team_abbrev=team, week_start=week_start, week_end=week_end, short_output=False, ) results_list = result.get("results", []) # Limit results results_list = results_list[:limit] if state.json_output: output_json(results_list) return if not results_list: console.print( f"[yellow]No results found for the given filters (Season {season})[/yellow]" ) return # Build table rows rows = [] for r in results_list: away_abbrev = safe_nested(r, "away_team", "abbrev") home_abbrev = safe_nested(r, "home_team", "abbrev") away_score = r.get("away_score", 0) home_score = r.get("home_score", 0) # Format score as "away_score-home_score" score_str = f"{away_score}-{home_score}" rows.append( [ r.get("week", ""), r.get("game_num", ""), away_abbrev, "@", home_abbrev, score_str, ] ) # Build title with filters title_parts = [f"Game Results (Season {season})"] if team: title_parts.append(f"Team: {team}") if week: title_parts.append(f"Week {week}") elif week_start and week_end: title_parts.append(f"Weeks {week_start}-{week_end}") title = " | ".join(title_parts) output_table(title, ["Week", "Gm", "Away", "@", "Home", "Score"], rows) except Exception as e: handle_error(e) @results_app.command("list") def results_list( team: Annotated[ Optional[str], typer.Option("--team", "-t", help="Filter by team abbreviation") ] = None, week: Annotated[ Optional[int], typer.Option("--week", "-w", help="Filter by specific week") ] = None, week_start: Annotated[ Optional[int], typer.Option("--week-start", help="Start week for range") ] = None, week_end: Annotated[ Optional[int], typer.Option("--week-end", help="End week for range") ] = None, season: Annotated[ Optional[int], typer.Option("--season", "-s", help="Season number") ] = None, limit: Annotated[ int, typer.Option("--limit", "-n", help="Max results to display") ] = 50, ): """ List game results with optional filters (explicit command). This is the same as calling 'majordomo results' without a subcommand. """ try: season = get_season(season) # Handle week vs week_start/week_end if week is not None: week_start = week week_end = week # Use low-level API to access all parameters result = state.api.get( "results", season=season, team_abbrev=team, week_start=week_start, week_end=week_end, short_output=False, ) results_list = result.get("results", []) # Limit results results_list = results_list[:limit] if state.json_output: output_json(results_list) return if not results_list: console.print( f"[yellow]No results found for the given filters (Season {season})[/yellow]" ) return # Build table rows rows = [] for r in results_list: away_abbrev = safe_nested(r, "away_team", "abbrev") home_abbrev = safe_nested(r, "home_team", "abbrev") away_score = r.get("away_score", 0) home_score = r.get("home_score", 0) # Format score as "away_score-home_score" score_str = f"{away_score}-{home_score}" rows.append( [ r.get("week", ""), r.get("game_num", ""), away_abbrev, "@", home_abbrev, score_str, ] ) # Build title with filters title_parts = [f"Game Results (Season {season})"] if team: title_parts.append(f"Team: {team}") if week: title_parts.append(f"Week {week}") elif week_start and week_end: title_parts.append(f"Weeks {week_start}-{week_end}") title = " | ".join(title_parts) output_table(title, ["Week", "Gm", "Away", "@", "Home", "Score"], rows) except Exception as e: handle_error(e)