from __future__ import annotations

from fastapi import APIRouter, status

from app.core.bootstrap import build_bootstrap_state, persist_bootstrap_config
from app.core.config import get_settings
from app.schemas.bootstrap import BootstrapSetupPayload, BootstrapStateRead

router = APIRouter(prefix="/bootstrap")


@router.get(
    "",
    response_model=BootstrapStateRead,
    summary="读取初始化状态",
    description="返回当前系统是否已完成初始化，以及公司、数据库和缓存配置快照。",
)
def get_bootstrap_state() -> BootstrapStateRead:
    return build_bootstrap_state(get_settings())


@router.post(
    "",
    response_model=BootstrapStateRead,
    status_code=status.HTTP_201_CREATED,
    summary="写入初始化配置",
    description="保存系统初始化配置，并刷新运行时数据库连接。",
)
def initialize_bootstrap(payload: BootstrapSetupPayload) -> BootstrapStateRead:
    return persist_bootstrap_config(payload, get_settings())
