import logging

from fastapi import APIRouter, HTTPException, Query
from pydantic import BaseModel

from app.services.sba_api_client import sba_api_client

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

router = APIRouter()


class TeamResponse(BaseModel):
    """Team information response"""

    id: int
    abbrev: str
    sname: str
    lname: str
    color: str | None = None
    thumbnail: str | None = None
    manager_legacy: str | None = None
    gmid: str | None = None
    gmid2: str | None = None
    division: dict | None = None


@router.get("/", response_model=list[TeamResponse])
async def get_teams(season: int = Query(..., description="Season number (e.g., 3)")):
    """
    Get all active teams for a season from SBA API.

    Args:
        season: Season number to fetch teams for

    Returns:
        List of active teams (excludes IL teams)
    """
    try:
        logger.info(f"Fetching teams for season {season}")
        teams = await sba_api_client.get_teams(season=season, active_only=True)

        return [
            TeamResponse(
                id=team["id"],
                abbrev=team["abbrev"],
                sname=team["sname"],
                lname=team["lname"],
                color=team.get("color"),
                thumbnail=team.get("thumbnail"),
                manager_legacy=team.get("manager_legacy"),
                gmid=team.get("gmid"),
                gmid2=team.get("gmid2"),
                division=team.get("division"),
            )
            for team in teams
        ]

    except Exception as e:
        logger.error(f"Failed to fetch teams: {e}", exc_info=True)
        raise HTTPException(status_code=500, detail="Failed to fetch teams")


@router.get("/{team_id}/roster")
async def get_team_roster(
    team_id: int,
    season: int = Query(..., description="Season number (e.g., 3)"),
):
    """
    Get roster for a specific team from SBA API.

    Args:
        team_id: Team ID
        season: Season number to fetch roster for

    Returns:
        List of players on the team's roster
    """
    try:
        logger.info(f"Fetching roster for team {team_id}, season {season}")
        roster = await sba_api_client.get_roster(team_id=team_id, season=season)

        return {"count": len(roster), "players": roster}

    except Exception as e:
        logger.error(f"Failed to fetch roster for team {team_id}: {e}", exc_info=True)
        raise HTTPException(
            status_code=500, detail=f"Failed to fetch roster for team {team_id}"
        )
