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

from clarabot_nano.common.config.constants import CommonConfigConstants
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.base.handler.base import AbstractNanoServerHandler
from clarabot_nano.server.general.process import GeneralServerProcess
from clarabot_nano.utility.path.resolve import resolve_path


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()
