import argparse
import asyncio
import os

from clarabot_nano.common.config.constants import CommonConfigConstants
from clarabot_nano.common.request.constants import IDEMPOTENT_HANDLER_KEYS
from clarabot_nano.common.spawn.constants import ShortNames
from clarabot_nano.development.common import load_development_server_config
from clarabot_nano.development.spawn.common import write_spawn_debug_info
from clarabot_nano.development.spawn.server.general.dbg_handlers import DebugServerEchoCHandler
from clarabot_nano.gateway.process import GatewayProcess
from clarabot_nano.utility.path.resolve import resolve_path

# allow testing multipart responses by extending the idempotent set
IDEMPOTENT_HANDLER_KEYS.add(DebugServerEchoCHandler.HANDLER_KEY)


async def startup(development_server_config):
    load_development_server_config(development_server_config)
    proc = GatewayProcess()
    config_dir = resolve_path(proc.get_config()["local"]["config_dir"], strict=True)
    write_spawn_debug_info(config_dir, os.getpid(), ShortNames.GATEWAY)
    await proc.run()


def main():
    CommonConfigConstants.DEVELOPMENT = True
    parser = argparse.ArgumentParser("Clarabot Nano - Test Gateway process")
    parser.add_argument("development_server_config")
    args = parser.parse_args()
    asyncio.run(startup(args.development_server_config))


if __name__ == "__main__":
    main()
