#include "config.hpp"
#include "service_runner.hpp"
#include "server_env.hpp"
#include "templates.hpp"
#include "config.hpp"
#include "services.hpp"
#include "utils/directories.hpp"
#include <boost/filesystem.hpp>
#include <iostream>
#include <fstream>
#include <sstream>
#include <cstdlib>
#include <chrono>
#include <iomanip>

namespace fs = boost::filesystem;

namespace dropshell {


service_runner::service_runner() : m_server_name(""), m_server_env(nullptr) {}

bool service_runner::init(const std::string& server_name, const std::string& service_name) {
    if (server_name.empty() || service_name.empty())
        return false;

    // Initialize server environment
    try {
        m_server_env = std::make_unique<server_env>(server_name);
        if (!m_server_env->is_valid()) {
            std::cerr << "Error: Invalid server environment" << std::endl;
            m_server_env.reset();
            return false;
        }
    } catch (const std::exception& e) {
        std::cerr << "Error: Failed to initialize server environment: " << e.what() << std::endl;
        return false;
    }

    m_server_name = server_name;
    m_service_info = get_service_info(server_name, service_name);
    
    mRemote_service_path = get_remote_service_path(m_server_name, m_service_info.service_name);
    mRemote_service_config_path = get_remote_service_config_path(m_server_name, m_service_info.service_name);
    mRemote_service_template_path = get_remote_service_template_path(m_server_name, m_service_info.service_name);
    mRemote_service_env_file = get_remote_service_env_file(m_server_name, m_service_info.service_name);
    
    return !m_service_info.path.empty();
}

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

bool service_runner::check_remote_dir_exists(const std::string& dir_path) const {
    std::string check_dir_cmd = construct_ssh_cmd() + "'test -d " + dir_path + "'";
    if (system(check_dir_cmd.c_str()) != 0) {
        std::cerr << "Error: Directory not found on remote server:" << dir_path << std::endl;
        return false;
    }
    return true;
}

bool service_runner::check_remote_file_exists(const std::string& file_path) const {
    std::string check_cmd = construct_ssh_cmd() + "'test -f " + file_path + "'";
    if (system(check_cmd.c_str()) != 0) {
        std::cerr << "Error: File not found on remote server: " << file_path << std::endl;
        return false;
    }
    return true;
}

bool service_runner::execute_ssh_command(const std::string& command, const std::string& error_msg) const {
    std::string full_cmd = construct_ssh_cmd() + command;
    return execute_local_command(full_cmd, error_msg);
}
bool service_runner::execute_local_command(const std::string& command, const std::string& error_msg) const {
    bool okay = (system(command.c_str()) == 0);

    if (!okay && !error_msg.empty())
        std::cerr << "Error: " << error_msg << std::endl;
    return okay;
}

void service_runner::maketitle(const std::string& title) const {
    std::cout << std::string(title.length() + 4, '-') << std::endl;
    std::cout << "| " << title << " |" << std::endl;
    std::cout << std::string(title.length() + 4, '-') << std::endl;
}

bool service_runner::install() {
    maketitle("Installing " + m_service_info.service_name + " (" + m_service_info.template_name + ") on " + m_server_name);
    if (!m_server_env) {
        std::cerr << "Error: Server service not initialized" << std::endl;
        return false;
    }
    
    // Check if template exists
    template_manager tm;
    template_info tinfo;
    if (!tm.get_template_info(m_service_info.template_name, tinfo)) {
        std::cerr << "Error: Template '" << m_service_info.template_name << "' not found" << std::endl;
        return false;
    }
        
    // Create service directory
    std::string mkdir_cmd = "'mkdir -p " + mRemote_service_path + "'";
    if (!execute_ssh_command(mkdir_cmd, "Failed to create service directory")) {
        return false;
    }
    
    // Check if rsync is installed on remote host
    std::string check_rsync_cmd = "'which rsync > /dev/null 2>&1'";
    if (!execute_ssh_command(check_rsync_cmd, "rsync is not installed on the remote host")) {
        return false;
    }

    // Copy template files
    {
        std::cout << "Copying template files from " << tinfo.path << " to " << mRemote_service_template_path << "/" << std::endl;
        std::string rsync_cmd = "rsync --delete -zrpc -e 'ssh -p " + m_server_env->get_SSH_PORT() + "' " + 
                            tinfo.path + "/ " + 
                            m_server_env->get_SSH_USER() + "@" + m_server_env->get_SSH_HOST() + ":" + 
                            mRemote_service_template_path + "/";
        execute_local_command(rsync_cmd,"Failed to copy template files");
    }
    
    // Copy service files (including service.env)
    {
        std::string local_service_path = get_local_service_path(m_server_name, m_service_info.service_name);
        if (local_service_path.empty() || !fs::exists(local_service_path)) {
            std::cerr << "Error: Service directory not found: " << local_service_path << std::endl;
            return false;
        }
        std::cout << "Copying service files from " << local_service_path << " to " << mRemote_service_config_path << std::endl; 
        std::string rsync_cmd = "rsync --delete -zrpc -e 'ssh -p " + m_server_env->get_SSH_PORT() + "' " + 
                            local_service_path + "/ " + 
                            m_server_env->get_SSH_USER() + "@" + m_server_env->get_SSH_HOST() + ":" + 
                            mRemote_service_config_path + "/";
        execute_local_command(rsync_cmd,"Failed to copy service files");
    }

    // Run install script
    {
        std::string install_cmd = "'cd " + mRemote_service_template_path + 
                                " && /bin/bash _install.sh " + mRemote_service_env_file + "'";
        bool ok= execute_ssh_command(install_cmd, "Failed to run install script");
        if (!ok)
            return false;
    }

    // print health tick
    std::cout << "Health: " << healthtick() << std::endl;
    return true;
}

bool service_runner::run_command(const std::string& command) {
    if (!m_server_env) {
        std::cerr << "Error: Server service not initialized" << std::endl;
        return false;
    }
    
    std::string script_path = mRemote_service_template_path + "/" + command + ".sh";
    
    // Check if service directory exists
    if (!check_remote_dir_exists(mRemote_service_path)) {
        return false;
    }
    
    // Check if command script exists
    if (!check_remote_file_exists(script_path)) {
        return false;
    }
    
    // Check if env file exists
    if (!check_remote_file_exists(mRemote_service_env_file)) {
        return false;
    }
    
    // Run the command
    std::string run_cmd = "'cd " + mRemote_service_template_path + 
                         " && /bin/bash " + script_path + " " + mRemote_service_env_file + "'";
    return execute_ssh_command(run_cmd, "Command returned error code: " + script_path);
}

bool service_runner::backup() {
    maketitle("Backing up " + m_service_info.service_name + " (" + m_service_info.template_name + ") on " + m_server_name);

    if (!m_server_env) {
        std::cerr << "Error: Server service not initialized" << std::endl;
        return false;
    }
    
    std::string script_path = mRemote_service_template_path + "/_backup.sh";
    
    // Check if basic installed stuff is in place.
    if (!check_remote_dir_exists(mRemote_service_path) || !check_remote_file_exists(script_path) || !check_remote_file_exists(mRemote_service_env_file))
        return false;

    // Create backups directory on server if it doesn't exist
    std::string server_backups_dir = m_server_env->get_DROPSHELL_DIR() + "/backups";
    std::string mkdir_cmd = "'mkdir -p " + server_backups_dir + "'";
    if (!execute_ssh_command(mkdir_cmd, "Failed to create backups directory on server")) {
        return false;
    }

    // Create backups directory locally if it doesn't exist
    std::string local_backups_dir = get_local_config_backups_path();
    if (!fs::exists(local_backups_dir)) {
        fs::create_directories(local_backups_dir);
    }

    // Get current datetime for backup filename
    auto now = std::chrono::system_clock::now();
    auto time = std::chrono::system_clock::to_time_t(now);
    std::stringstream datetime;
    datetime << std::put_time(std::localtime(&time), "%Y-%m-%d_%H-%M-%S");
    
    // Construct backup filename
    std::string backup_filename = m_server_name + "-" + m_service_info.service_name + "-" + datetime.str() + ".tgz";
    std::string server_backup_path = server_backups_dir + "/" + backup_filename;
    std::string local_backup_path = (fs::path(local_backups_dir) / backup_filename).string();

    // Run backup script
    std::string backup_cmd = "'cd " + mRemote_service_template_path + 
                           " && /bin/bash \""+script_path+"\" " + mRemote_service_env_file + " " + server_backup_path + "'";
    if (!execute_ssh_command(backup_cmd, "Backup script failed")) {
        return false;
    }

    // Copy backup file from server to local
    std::string scp_cmd = "scp -P " + m_server_env->get_SSH_PORT() + " " + 
                         m_server_env->get_SSH_USER() + "@" + m_server_env->get_SSH_HOST() + ":" + 
                         server_backup_path + " " + local_backup_path;
    if (!execute_local_command(scp_cmd, "Failed to copy backup file from server")) {
        return false;
    }

    std::cout << "Backup created successfully: " << local_backup_path << std::endl;
    return true;
}

service_runner::HealthStatus service_runner::is_healthy()
{
    if (!m_server_env) {
        std::cerr << "Error: Server service not initialized" << std::endl;
        return HealthStatus::ERROR;
    }

    // Check if status script exists
    std::string script_path = mRemote_service_template_path + "/_status.sh";
    if (!check_remote_file_exists(script_path)) {
        return HealthStatus::NOTINSTALLED;
    }
    
    // Run status script, does not display output.
    bool ok = execute_ssh_command("'cd " + mRemote_service_template_path + " && /bin/bash _status.sh " + mRemote_service_env_file + " > /dev/null 2>&1'", "");
    if (!ok)
        return HealthStatus::UNHEALTHY;

    return HealthStatus::HEALTHY;
}

std::string service_runner::healthtick()
{
    std::string green_tick = "\033[32m✓\033[0m";
    std::string red_cross = "\033[31m✗\033[0m";    
    std::string yellow_exclamation = "\033[33m!\033[0m";
    
    HealthStatus status = is_healthy();
    if (status == HealthStatus::HEALTHY)
        return green_tick;
    else if (status == HealthStatus::UNHEALTHY)
        return red_cross;
    else
        return yellow_exclamation;
}

std::vector<int> service_runner::get_ports()
{
    std::vector<int> ports;
    if (!m_server_env) {
        std::cerr << "Error: Server service not initialized" << std::endl;
        return ports;
    }
    
    std::string script_path = mRemote_service_template_path + "/_ports.sh";
        
    // Check if ports script exists
    if (!check_remote_file_exists(script_path)) {
        return ports;
    }
    
    // Run the ports script and capture output
    std::string run_cmd = "'cd " + mRemote_service_template_path + 
                         " && /bin/bash " + script_path + " " + mRemote_service_env_file + "'";
    
    // Create a temporary file to store the output
    std::string temp_file = "/tmp/dropshell_ports_" + std::to_string(getpid());
    std::string full_cmd = construct_ssh_cmd() + run_cmd + " > " + temp_file;
    
    if (!execute_local_command(full_cmd, "Failed to run ports script")) {
        return ports;
    }
    
    // Read the output file
    std::ifstream in(temp_file);
    if (!in.is_open()) {
        std::cerr << "Error: Failed to read ports output" << std::endl;
        return ports;
    }
    
    // Read the entire file content
    std::string content((std::istreambuf_iterator<char>(in)), std::istreambuf_iterator<char>());
    
    // Clean up temporary file
    std::remove(temp_file.c_str());
    
    // Process the content
    std::stringstream ss(content);
    std::string token;
    
    // First split by commas
    while (std::getline(ss, token, ',')) {
        // Then split each comma-separated part by whitespace
        std::stringstream token_ss(token);
        std::string port_str;
        while (token_ss >> port_str) {  // This handles all whitespace including newlines
            try {
                int port = std::stoi(port_str);
                ports.push_back(port);
            } catch (const std::exception& e) {
                std::cerr << "Warning: Invalid port number: " << port_str << std::endl;
            }
        }
    }
    
    return ports;
}

} // namespace dropshell