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

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_welcome_message(
    request: Request,
    db=Depends(get_system_db_dep),
):
    conn, cur = db

    try:
        await cur.execute("SELECT message, updated_at FROM welcome_messages WHERE status = 1 ORDER BY updated_at DESC LIMIT 1")
        row = await cur.fetchone()
        if row:
            return {"message": row["message"], "updated_at": row["updated_at"]}
    except Exception:
        pass

    return {"message": "欢迎使用综合执法智能辅助系统", "updated_at": None}
