"""
SBA Schedule API routes.

Provides endpoints to fetch current season/week and scheduled games
from the SBA production API.
"""

import logging
from typing import Any

from fastapi import APIRouter, HTTPException

from app.services.sba_api_client import sba_api_client

logger = logging.getLogger(f"{__name__}.schedule")

router = APIRouter()


@router.get("/current")
async def get_current() -> dict[str, Any]:
    """
    Get current season and week from SBA API.

    Returns:
        Dictionary with season and week numbers

    Example response:
        {"season": 13, "week": 12}
    """
    try:
        return await sba_api_client.get_current()
    except Exception as e:
        logger.error(f"Failed to get current season/week: {e}")
        raise HTTPException(
            status_code=502, detail="Failed to fetch current season/week from SBA API"
        )


@router.get("/games")
async def get_schedule_games(
    season: int,
    week: int,
) -> list[dict[str, Any]]:
    """
    Get scheduled games for a specific week from SBA API.

    Args:
        season: Season number (e.g., 13)
        week: Week number within the season

    Returns:
        List of game dictionaries with team matchup info

    Example response:
        [
            {
                "id": 12345,
                "season": 13,
                "week": 12,
                "home_team_id": 35,
                "away_team_id": 42,
                "home_abbrev": "STL",
                "away_abbrev": "CHC",
                ...
            }
        ]
    """
    try:
        games = await sba_api_client.get_schedule_games(
            season=season, week_start=week, week_end=week
        )
        return games
    except Exception as e:
        logger.error(f"Failed to get schedule games for S{season} W{week}: {e}")
        raise HTTPException(
            status_code=502,
            detail=f"Failed to fetch schedule games from SBA API for season {season}, week {week}",
        )
