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

from app.dependencies import get_db, get_current_user
from app.models.user import User
from app.models.credit_ratio import CreditRatio
from app.models.video_engine import VideoEngine
from app.models.image_engine import ImageEngine
from app.schemas.credit import CreditBalanceOut, CreditRecordOut
from app.schemas.credit_ratio import CreditRatioOut
from app.services.credit_ratio_service import list_all_credit_ratios
from app.services.credits import get_records

router = APIRouter(prefix="/credits", tags=["credits"])


@router.get("")
async def get_credits(
    page: int = Query(1, ge=1),
    page_size: int = Query(20, ge=1, le=100),
    current_user: User = Depends(get_current_user),
    db: AsyncSession = Depends(get_db),
):
    records, total = await get_records(db, current_user.id, page, page_size)
    return {
        "credits": round(current_user.credits, 2),
        "records": [CreditRecordOut.model_validate(r) for r in records],
        "total": total,
    }


@router.get(
    "/credit-ratios",
    response_model=list[CreditRatioOut],
    summary="获取积分比例列表",
    description="客户端获取当前系统配置的积分计费规则列表。普通登录用户可访问，只读返回 credit_ratios 表中的图片/视频积分比例配置。",
)
async def list_client_credit_ratios(
    current_user: User = Depends(get_current_user),
    db: AsyncSession = Depends(get_db),
):
    return await list_all_credit_ratios(db)


@router.get("/ratios", response_model=dict)
async def get_credit_ratios(
    current_user: User = Depends(get_current_user),
    db: AsyncSession = Depends(get_db),
):
    async def get_ratios_for_engine_type(gen_type: str, engine_ids: list):
        for engine_id in engine_ids:
            result = await db.execute(
                select(CreditRatio)
                .where(CreditRatio.gen_type == gen_type)
                .where(CreditRatio.model_config_id == engine_id)
            )
            ratios = result.scalars().all()
            if ratios:
                return [CreditRatioOut.model_validate(r) for r in ratios]
        return []
    
    video_engines_result = await db.execute(
        select(VideoEngine.id)
        .where(VideoEngine.is_active == True)
        .order_by(VideoEngine.priority.desc())
    )
    video_engine_ids = video_engines_result.scalars().all()
    
    image_engines_result = await db.execute(
        select(ImageEngine.id)
        .where(ImageEngine.is_active == True)
        .order_by(ImageEngine.priority.desc())
    )
    image_engine_ids = image_engines_result.scalars().all()
    
    grouped = {}
    
    video_ratios = await get_ratios_for_engine_type("video", video_engine_ids)
    if video_ratios:
        grouped["video"] = video_ratios
    
    image_ratios = await get_ratios_for_engine_type("image", image_engine_ids)
    if image_ratios:
        grouped["image"] = image_ratios
    
    return grouped
