#include "command_registry.hpp"
#include "config.hpp"
#include "utils/utils.hpp"
#include "utils/directories.hpp"
#include "shared_commands.hpp"
#include "server_env_manager.hpp"
#include "services.hpp"
#include "servers.hpp"
#include "transwarp.hpp"

namespace dropshell
{

    int health_handler(const CommandContext &ctx);

    static std::vector<std::string> health_name_list = {"health", "check", "healthcheck", "status"};

    // Static registration
    struct HealthCommandRegister
    {
        HealthCommandRegister()
        {
            CommandRegistry::instance().register_command({health_name_list,
                                                          health_handler,
                                                          shared_commands::std_autocomplete_allowall,
                                                          false, // hidden
                                                          true, // requires_config
                                                          true, // requires_install
                                                          1,     // min_args (after command)
                                                          2,     // max_args (after command)
                                                          "health SERVER",
                                                          "Check the health of a server.",
                                                          R"(
                health <server>
                )"});
        }
    } health_command_register;



    // ------------------------------------------------------------------------------------------------
    // health command implementation
    // ------------------------------------------------------------------------------------------------
    int health_handler(const CommandContext &ctx)
    {
        if (ctx.args.size() < 1)
        {
            error << "Server name is required" << std::endl;
            return 1;
        }

        std::string server = safearg(ctx.args, 0);

        if (ctx.args.size() == 1) {
            // get all services on server
            std::vector<LocalServiceInfo> services = get_server_services_info(server);
            transwarp::parallel exec{services.size()};
            auto task = transwarp::for_each(exec, services.begin(), services.end(), [&](const LocalServiceInfo& service) {
                std::string status = shared_commands::healthtick(server, service.service_name);
                std::cout << status << " " << service.service_name << " (" << service.template_name << ")" << std::endl << std::flush;
            });
            task->wait();
            return 0;
        } else {
            // get service status
            std::string service = safearg(ctx.args, 1);
            LocalServiceInfo service_info = get_service_info(server, service);
            std::cout << shared_commands::healthtick(server, service) << " " << service << " (" << service_info.template_name << ")" << std::endl << std::flush;
        }
        return 0;
    }

} // namespace dropshell