from fastapi import APIRouter, Depends
from sqlalchemy.ext.asyncio import AsyncSession

from app.dependencies import get_db
from app.repositories.tag_repo import TagRepository

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


@router.get("/tags")
async def list_tags(
    q: str | None = None,
    limit: int = 100,
    offset: int = 0,
    sort: str = "name",
    min_count: int = 0,
    db: AsyncSession = Depends(get_db),
):
    limit = min(limit, 500)
    tag_repo = TagRepository(db)
    items, total = await tag_repo.list_tags(
        prefix=q, limit=limit, offset=offset, sort=sort, min_count=min_count
    )
    return {"items": items, "total": total, "limit": limit, "offset": offset}
