"""Similar tracks endpoints."""
from fastapi import APIRouter, Depends, HTTPException, Query
from sqlalchemy.orm import Session
from uuid import UUID

from ...models.database import get_db
from ...models import crud

router = APIRouter()


@router.get("/tracks/{track_id}/similar")
async def get_similar_tracks(
    track_id: UUID,
    limit: int = Query(10, ge=1, le=50),
    db: Session = Depends(get_db),
):
    """Get tracks similar to the given track.

    Args:
        track_id: Reference track UUID
        limit: Maximum results
        db: Database session

    Returns:
        List of similar tracks

    Raises:
        HTTPException: 404 if track not found
    """
    # Check if reference track exists
    ref_track = crud.get_track_by_id(db, track_id)

    if not ref_track:
        raise HTTPException(status_code=404, detail="Track not found")

    # Get similar tracks
    similar_tracks = crud.get_similar_tracks(db, track_id, limit=limit)

    return {
        "reference_track_id": str(track_id),
        "similar_tracks": [track.to_dict() for track in similar_tracks],
        "total": len(similar_tracks),
    }
