import argparse
import asyncio
import os
from typing import Sequence, Type

from clarabot_nano_common.common.config.constants import CommonConfigConstants
from clarabot_nano_common.server.base.handler.base import AbstractNanoServerHandler
from clarabot_nano_common.utility.path.resolve import resolve_path

from clarabot_nano.common.spawn.constants import ShortNames
from clarabot_nano.development.spawn.common import write_spawn_debug_info
from clarabot_nano.development.spawn.server.general.dbg_handlers import (
    DebugServerEchoSPHandler,
    DebugServerEchoCHandler,
    DebugServerEchoCAdminHandler,
)
from clarabot_nano.server.general.process import GeneralServerProcess


class _TestingServerProcess(GeneralServerProcess):
    def _init_get_handler_classes(self) -> Sequence[Type[AbstractNanoServerHandler]]:
        hs = [DebugServerEchoSPHandler, DebugServerEchoCHandler, DebugServerEchoCAdminHandler]
        hs.extend(GeneralServerProcess._init_get_handler_classes(self))
        return hs


async def startup():
    with _TestingServerProcess.create(ShortNames.GENERAL_SERVER) as proc:
        config_dir = resolve_path(proc.get_config()["local"]["config_dir"], strict=True)
        write_spawn_debug_info(config_dir, os.getpid(), ShortNames.GENERAL_SERVER)
        await proc.run()


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


if __name__ == "__main__":
    main()
