#include "server_env_manager.hpp"
#include "utils/envmanager.hpp"
#include "utils/directories.hpp"
#include "utils/utils.hpp"
#include "services.hpp"
#include "contrib/base64.hpp"
#include "templates.hpp"

#include <iostream>
#include <memory>
#include <filesystem>

namespace dropshell {

server_env_manager::server_env_manager(const std::string& server_name) : mValid(false), mServer_name(server_name) {
    if (server_name.empty())
        return;
        
    // Construct the full path to server.env
    std::string env_path = localfile::server_env(server_name);
    
    // Check if file exists
    if (!std::filesystem::exists(env_path)) {
        std::cerr << "Server environment file not found: " + env_path << std::endl;
        return;
    }

    try {
        // Use envmanager to handle the environment file
        m_env_manager = std::unique_ptr<envmanager>(new envmanager(env_path));
        m_env_manager->load();
        
        // Get all variables
        m_env_manager->get_all_variables_substituted(variables);

        // Verify required variables exist
        for (const auto& var : {"SSH_HOST", "SSH_USER", "SSH_PORT", "DROPSHELL_DIR"}) {
            if (variables.find(var) == variables.end()) {
                // Print the variables identified in the file
                std::cout << "Variables identified in the file:" << std::endl;
                for (const auto& v : variables) {
                    std::cout << "    " << v.first << std::endl;
                }
                throw std::runtime_error("Missing required variable: " + std::string(var));
            }
        }
        mValid = true;

    } catch (const std::exception& e) {
        std::cerr << "Failed to parse server environment file: " + std::string(e.what()) << std::endl;
    }    
}

std::string server_env_manager::get_variable(const std::string& name) const {
    if (!m_env_manager) {
        return "";
    }
    return m_env_manager->get_variable_substituted(name);
}

// Helper method implementations
std::string server_env_manager::construct_ssh_cmd() const {
    std::stringstream ssh_cmd;
    ssh_cmd << "ssh -p " << get_SSH_PORT() << " "
            << get_SSH_USER() << "@" << get_SSH_HOST();
    return ssh_cmd.str();
}

std::string server_env_manager::construct_standard_command_run_cmd(const std::string &service_name, const std::string &command, std::vector<std::string> args, bool silent) const
{
    std::string remote_service_template_path = remotepath::service_template(mServer_name,service_name);
    std::string remote_service_config_path = remotepath::service_config(mServer_name,service_name);

    std::string script_path = remote_service_template_path + "/" + command + ".sh";

    std::map<std::string, std::string> env_vars;
    get_all_service_env_vars(service_name, env_vars);

    std::string argstr = "";
    for (const auto& arg : args) {
        argstr += " " + quote(dequote(trim(arg)));
    }

    sCommand scommand(remote_service_template_path, "bash " + quote(script_path) + argstr + (silent ? " > /dev/null 2>&1" : ""), env_vars);
    std::string run_cmd = scommand.construct_safecmd();
    return run_cmd;
}

void server_env_manager::get_all_service_env_vars(const std::string &service_name, std::map<std::string, std::string> & all_env_vars) const
{
    all_env_vars.clear();

    // add in some handy variables.
    all_env_vars["CONFIG_PATH"] = remotepath::service_config(mServer_name,service_name);
    all_env_vars["SERVER"] = mServer_name;
    all_env_vars["SERVICE"] = service_name;


    { // load service.env from the service on this machine.
    std::map<std::string, std::string> env_vars;
    envmanager env_manager(localfile::service_env(mServer_name,service_name));
    env_manager.load();
    env_manager.get_all_variables(env_vars);
    all_env_vars.merge(env_vars);
    }

    { // load _default.env from the template on this machine.
    std::map<std::string, std::string> env_vars;
    ServiceInfo service_info = get_service_info(mServer_name, service_name);
    std::string defaultenvpath = service_info.local_template_default_env_path;
    if (std::filesystem::exists(defaultenvpath)) {
        envmanager env_manager(defaultenvpath);
        env_manager.load();
        env_manager.get_all_variables(env_vars);
        all_env_vars.merge(env_vars);
        }
    else 
        std::cerr << "Warning: _default.env not found in template: " << defaultenvpath << std::endl;
    }
}


bool server_env_manager::check_remote_dir_exists(const std::string &dir_path) const
{
    sCommand scommand("test -d " + quote(dir_path));
    return execute_ssh_command(scommand);
}

bool server_env_manager::check_remote_file_exists(const std::string& file_path) const {
    sCommand scommand("test -f " + quote(file_path));
    return execute_ssh_command(scommand);
}

bool server_env_manager::check_remote_items_exist(const std::vector<std::string> &file_paths) const
{
    // convert file_paths to a single string, separated by spaces
    std::string file_paths_str;
    std::string file_names_str;
    for (const auto& file_path : file_paths) {
        file_paths_str += quote(file_path) + " ";
        file_names_str += std::filesystem::path(file_path).filename().string() + " ";
    }
    // check if all items in the vector exist on the remote server, in a single command.
    sCommand scommand("for item in " + file_paths_str + "; do test -f $item; done");

    bool okay = execute_ssh_command(scommand);
    if (!okay) {
        std::cerr << "Error: Required items not found on remote server: " << file_names_str << std::endl;
        return false;
    }
    return true;
}

bool server_env_manager::execute_ssh_command(const sCommand& command) const {
    std::string full_cmd = construct_ssh_cmd() + " " + quote(command.construct_safecmd());
    return execute_local_command(full_cmd);
}

bool server_env_manager::execute_ssh_command_and_capture_output(const sCommand& command, std::string &output) const
{
    std::string full_cmd = construct_ssh_cmd() + " " + quote(command.construct_safecmd());
    return execute_local_command_and_capture_output(full_cmd, output);
}

bool server_env_manager::run_remote_template_command(const std::string &service_name, const std::string &command, std::vector<std::string> args, bool silent) const
{
    std::string full_cmd = construct_standard_command_run_cmd(service_name, command, args, silent);
    return execute_ssh_command(full_cmd);
}

bool server_env_manager::run_remote_template_command_and_capture_output(const std::string &service_name, const std::string &command, std::vector<std::string> args, std::string &output, bool silent) const
{
    std::string full_cmd = construct_standard_command_run_cmd(service_name, command, args, silent);
    return execute_ssh_command_and_capture_output(full_cmd, output);
}

bool server_env_manager::execute_local_command(const sCommand& command) {
    return (system(command.construct_safecmd().c_str()) == 0);
}

bool server_env_manager::execute_local_command_and_capture_output(const sCommand& command, std::string &output)
{
    std::string full_cmd = command.construct_safecmd() + " 2>&1";
    FILE *pipe = popen(full_cmd.c_str(), "r");
    if (!pipe) {
        return false;
    }
    char buffer[128];
    while (fgets(buffer, sizeof(buffer), pipe) != nullptr) {
        output += buffer;
    }
    int status = pclose(pipe);
    return (status == 0);
}


std::string sCommand::construct_safecmd() const
{
    std::string to_encode;

    for (const auto& env_var : mVars) {
        to_encode += env_var.first + "=" + quote(dequote(trim(env_var.second))) + " ";
    }
    to_encode += mCmd;

    std::string encoded = base64_encode(to_encode);

    std::string commandstr = "echo " + encoded + " | base64 -d | bash";

    if (!mDir.empty()) 
        commandstr = "cd " + quote(mDir) + " && " + commandstr;
    return commandstr;
}

// base64 <<< "FOO=BAR WHEE=YAY bash ./test.sh"
// echo YmFzaCAtYyAnRk9PPUJBUiBXSEVFPVlBWSBiYXNoIC4vdGVzdC5zaCcK | base64 -d | bash

std::string makesafecmd(const std::string &command)
{
    std::string encoded = base64_encode(dequote(trim(command)));
    std::string commandstr = "echo " + encoded + " | base64 -d | bash";
    return commandstr;
}

} // namespace dropshell