#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), mServerName(server_name) {
    if (server_name.empty())
        return;
        
    // Construct the full path to server.env
    std::string server_env_path = localfile::server_env(server_name);
    
    // Check if file exists
    if (!std::filesystem::exists(server_env_path)) {
        std::cerr << "Server environment file not found: " + server_env_path << std::endl;
        return;
    }

    try {
        // Use envmanager to handle the environment file
        envmanager env_manager(server_env_path);
        env_manager.load();
        
        // Get all variables
        env_manager.get_all_variables_substituted(mVariables);

        // Verify required variables exist
        for (const auto& var : {"SSH_HOST", "SSH_USER", "SSH_PORT", "DROPSHELL_DIR"}) {
            if (mVariables.find(var) == mVariables.end()) {
                // Print the variables identified in the file
                std::cout << "Variables identified in the file:" << std::endl;
                for (const auto& v : mVariables) {
                    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 {
    auto it = mVariables.find(name);
    if (it == mVariables.end()) {
        return "";
    }
    return it->second;
}

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

sCommand 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(mServerName,service_name);
    std::string remote_service_config_path = remotepath::service_config(mServerName,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(mServerName, 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);
    return scommand;
}


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, bool allocateTTY) const {
    std::string full_cmd = construct_ssh_cmd(allocateTTY) + " " + (allocateTTY ? halfquote(command.construct_rawcmd()) : quote(command.construct_safecmd()));
    return (system(full_cmd.c_str()) == 0);
}

bool server_env_manager::execute_ssh_command_and_capture_output(const sCommand& command, std::string &output, bool allocateTTY) const
{
    std::string full_cmd = construct_ssh_cmd(allocateTTY) + " " + 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
{
    sCommand scommand = construct_standard_command_run_cmd(service_name, command, args, silent);
    bool allocateTTY = (command=="ssh");
    return execute_ssh_command(scommand, allocateTTY);
}

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
{
    sCommand scommand = construct_standard_command_run_cmd(service_name, command, args, silent);
    bool allocateTTY = (command=="ssh");
    return execute_ssh_command_and_capture_output(scommand, output, allocateTTY);
}

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;
}

std::string sCommand::construct_rawcmd() const
{
    std::string rawcmd = "cd " + quote(mDir) + " && ";

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

// 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