from nonebot import require, get_driver
from nonebot.plugin import PluginMetadata

from .config import Config

require("danding_bot.plugins.danding_points")

__plugin_meta__ = PluginMetadata(
    name="Group Horse Racing",
    description="Group horse racing plugin with betting and points integration",
    usage="Use /赛马 commands for horse racing gameplay",
    type="application",
    config=Config,
    extra={
        "required_plugins": ["danding_bot.plugins.danding_points"],
    },
)

# 从 NoneBot driver config 中提取本插件配置（去掉 GROUP_HORSE_RACING_ 前缀）
_nb_config = get_driver().config
_raw = _nb_config.model_dump() if hasattr(_nb_config, "model_dump") else _nb_config.dict()
_prefix = "group_horse_racing_"

plugin_config = Config(**{
    k[len(_prefix):].upper(): v
    for k, v in _raw.items()
    if k.startswith(_prefix)
})

from . import commands, test_commands  # noqa: F401, E402

# Register lifecycle hooks for room persistence
from .room_store import room_store

_driver = get_driver()

@_driver.on_startup
async def _on_startup():
    await room_store.load_rooms()

@_driver.on_shutdown
async def _on_shutdown():
    await room_store.close()
