from ...config import config
from ...utils import text_response, UserLocal
from ...command_matcher import MC
import tsugu_api
from ...utils import server_exists, server_name_2_server_id, server_id_2_server_name
from tsugu_api._typing import _ServerId, _Update
import typing
from ...storage.db import db_manager
import json


def handler(user: UserLocal, res: MC, platform: str, channel_id: str):
    server_mode: typing.Optional[_ServerId] = server_name_2_server_id(res.args[0]) if res.args else None
    if not server_mode:
        return text_response('未找到服务器，请输入正确的服务器名')
    cursor = db_manager.conn.cursor()
    cursor.execute("UPDATE users SET server_mode = ? WHERE user_id = ? AND platform = ?",
                   (server_mode, user.user_id, platform))
    db_manager.conn.commit()
    return text_response(f'服务器模式设置为 {server_id_2_server_name(server_mode)}')

