"""
封面背景图 API
===============

按脚本大类返回封面背景图列表。
"""

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

from app.db.session import get_db
from app.models.cover_background import CoverBackground
from app.schemas.common import ApiResponse, success_response
from app.schemas.cover_background import CoverBackgroundItem

router = APIRouter()


@router.get("/cover-backgrounds", response_model=ApiResponse[list[CoverBackgroundItem]])
async def list_cover_backgrounds(
    script_code: str,
    db: AsyncSession = Depends(get_db),
):
    """
    根据脚本大类 code 获取封面背景图列表

    如 script_code=bk 返回装修避坑大类下的所有封面背景图。
    """
    result = await db.execute(
        select(CoverBackground)
        .where(CoverBackground.script_code == script_code)
        .where(CoverBackground.status == "active")
        .order_by(CoverBackground.sort_order.asc())
    )

    items = [
        CoverBackgroundItem(
            id=m.id,
            script_code=m.script_code,
            title=m.title,
            url=m.url,
            sort_order=m.sort_order,
        )
        for m in result.scalars().all()
    ]

    return success_response(data=items, message="获取成功")
