"""方案计划文档管理系统（DMS）后端模块。"""

from __future__ import annotations

from flask import Flask

from dms.api.v1 import api_v1
from dms.api.v1.blueprint import (
    register_dms_response_headers,
    register_dms_routing_error_handlers,
)
from dms.config import load_dms_config
from dms.extensions import db
from dms.services.ui_dictionary_service import load_ui_dictionary_config
from dms.storage.paths import ensure_storage_directories
from dms.services.document_converter import create_converter


def init_dms(app: Flask) -> None:
    """初始化独立DMS基础设施并注册 ``/api/v1`` Blueprint。"""
    app.config.from_mapping(load_dms_config())
    app.extensions["dms_ui_dictionaries"] = load_ui_dictionary_config(
        app.config["DMS_UI_DICTIONARY_CONFIG_PATH"]
    )
    app.extensions["dms_document_converter"] = create_converter(app.config)
    db.init_app(app)

    # 导入模型以确保SQLAlchemy metadata完整；不触发数据库连接。
    import dms.models  # noqa: F401

    ensure_storage_directories(app.config["DMS_STORAGE_ROOT"])
    app.register_blueprint(api_v1)
    register_dms_routing_error_handlers(app)
    register_dms_response_headers(app)


__all__ = ["init_dms"]
