"""Search endpoints."""
from fastapi import APIRouter, Depends, Query
from sqlalchemy.orm import Session
from typing import Optional

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

router = APIRouter()


@router.get("")
async def search_tracks(
    q: str = Query(..., min_length=1, description="Search query"),
    genre: Optional[str] = None,
    mood: Optional[str] = None,
    limit: int = Query(100, ge=1, le=500),
    db: Session = Depends(get_db),
):
    """Search tracks by text query.

    Args:
        q: Search query string
        genre: Optional genre filter
        mood: Optional mood filter
        limit: Maximum results
        db: Database session

    Returns:
        List of matching tracks
    """
    tracks = crud.search_tracks(
        db=db,
        query=q,
        genre=genre,
        mood=mood,
        limit=limit,
    )

    return {
        "query": q,
        "tracks": [track.to_dict() for track in tracks],
        "total": len(tracks),
    }
