"""LLM usage routes"""

from fastapi import APIRouter, Request

router = APIRouter(tags=["llm"])


@router.get("/llm/usage")
async def get_usage(agent_name: str | None = None, req: Request = None):
    """Get LLM usage statistics"""
    llm_gateway = req.app.state.llm_gateway
    summary = llm_gateway.get_usage(agent_name=agent_name)
    return {
        "total_tokens": summary.total_tokens,
        "total_cost": summary.total_cost,
        "by_model": summary.by_model,
    }


@router.get("/llm/models")
async def list_models(req: Request):
    """List available LLM models from all configured providers."""
    llm_gateway = req.app.state.llm_gateway
    config = llm_gateway._config

    models = []
    for provider_name, provider_config in config.providers.items():
        for model_name, model_config in provider_config.models.items():
            models.append({
                "id": f"{provider_name}/{model_name}",
                "provider": provider_name,
                "model": model_name,
                "max_tokens": model_config.get("max_tokens"),
                "cost_per_1k_input": model_config.get("cost_per_1k_input"),
                "cost_per_1k_output": model_config.get("cost_per_1k_output"),
            })

    aliases = config.model_aliases if config.model_aliases else {}

    return {
        "models": models,
        "aliases": aliases,
        "default": aliases.get("default", models[0]["id"] if models else None),
    }
