"""
背景音乐 CRUD
============
"""

from sqlalchemy import select
from sqlalchemy.ext.asyncio import AsyncSession

from app.crud.base import CRUDBase
from app.models.bgm_music import BgmMusic


class BgmMusicCRUD(CRUDBase[BgmMusic]):
    """背景音乐数据访问"""

    def __init__(self) -> None:
        super().__init__(BgmMusic)

    async def get_active_by_category(
        self, db: AsyncSession, *, category: str | None = None
    ) -> list[BgmMusic]:
        """
        获取指定分类下状态为 active 的音乐列表

        Args:
            category: 场景分类，None 表示获取全部
        """
        query = select(BgmMusic).where(BgmMusic.status == "active")
        if category:
            query = query.where(BgmMusic.category == category)
        query = query.order_by(BgmMusic.sort_order.asc(), BgmMusic.id.asc())
        result = await db.execute(query)
        return list(result.scalars().all())


# 导出实例
bgm_music = BgmMusicCRUD()
