"""API endpoints for statistics."""

from fastapi import APIRouter, Depends
from sqlalchemy.orm import Session
from typing import List

from app.database import get_db
from app.schemas import StatisticsResponse, ToolUsageResponse, SessionResponse
from app.crud import get_statistics, get_tool_usage, get_sessions

router = APIRouter(prefix="/api/statistics", tags=["statistics"])


@router.get("", response_model=StatisticsResponse)
async def get_stats(db: Session = Depends(get_db)):
    """
    Get current statistics.

    Returns aggregated statistics including:
    - Total events count
    - Number of unique tools used
    - Number of agents spawned
    - Number of unique sessions
    - Last update timestamp
    """
    stats = get_statistics(db)
    return stats


@router.get("/tools", response_model=List[ToolUsageResponse])
async def get_tool_stats(db: Session = Depends(get_db)):
    """
    Get tool usage statistics.

    Returns a list of all tools used with their usage counts and timestamps.
    Sorted by usage count (descending).
    """
    tools = get_tool_usage(db)
    return tools


@router.get("/sessions", response_model=List[SessionResponse])
async def get_sessions_list(limit: int = 100, db: Session = Depends(get_db)):
    """
    Get list of sessions.

    Returns recent sessions with their metadata.
    """
    sessions = get_sessions(db, limit=limit)
    return sessions
