from fastapi import APIRouter, Depends, Request
from ...core.database import get_db, get_system_db, _has_split_databases
from ...core.permission import get_current_user

router = APIRouter()


async def get_system_db_dep():
    """FastAPI 依赖：优先使用 system.db，单库模式回退到 get_db()"""
    _db_gen = get_system_db() if _has_split_databases() else get_db()
    async for db in _db_gen:
        yield db


@router.get("")
async def get_enabled_quick_prompts(request: Request, db=Depends(get_system_db_dep)):
    current_user = get_current_user(request)
    if not current_user:
        return {"error": "未登录", "detail": "unauthorized"}

    conn, cur = db
    try:
        await cur.execute(
            "SELECT id, icon, label, template FROM quick_prompts WHERE enabled = 1 ORDER BY sort_order ASC, id ASC"
        )
        rows = await cur.fetchall()
        items = [dict(r) for r in rows]
        return {"list": items}
    except Exception:
        return {"list": []}
