#include "config.hpp"
#include "init_user_directory.hpp"
#include "server_service.hpp"
#include "server_env.hpp"
#include "templates.hpp"
#include "config.hpp"
#include <boost/filesystem.hpp>
#include <iostream>
#include <fstream>
#include <sstream>
#include <cstdlib>
#include <chrono>
#include <iomanip>

namespace fs = boost::filesystem;

namespace dropshell {

std::vector<std::string> get_server_services(const std::string& server_name) {
    std::vector<std::string> services;
    std::string user_dir;
    
    if (!get_user_directory(user_dir)) {
        std::cerr << "Error: User directory not set" << std::endl;
        return services;
    }
    
    fs::path server_dir = fs::path(user_dir) / "servers" / server_name;
    if (!fs::exists(server_dir)) {
        std::cerr << "Error: Server directory not found" << std::endl;
        return services;
    }
    
    // Look for .env files in the server directory
    for (const auto& entry : fs::directory_iterator(server_dir)) {
        if (entry.path().extension() == ".env" && entry.path().filename().string() != "_server.env") {
            services.push_back(entry.path().stem().string());
        }
    }
    
    return services;
}

server_service::server_service() : m_server_name(""), m_service_name(""), m_server_env(nullptr) {}

bool server_service::init(const std::string& server_name, const std::string& service_name) {
    std::string user_dir;
    if (!get_user_directory(user_dir)) {
        std::cerr << "Error: User directory not set" << std::endl;
        return false;
    }
    
    // Check if server exists
    fs::path server_dir = fs::path(user_dir) / "servers" / server_name;
    if (!fs::exists(server_dir)) {
        std::cerr << "Error: Server '" << server_name << "' not found" << std::endl;
        return false;
    }
    
    // Check if service env file exists
    fs::path service_env = server_dir / (service_name + ".env");
    if (!fs::exists(service_env)) {
        std::cerr << "Error: Service environment file not found: " << service_env.string() << std::endl;
        return false;
    }
    
    // Initialize server environment
    try {
        m_server_env = std::make_unique<server_env>(server_dir.string());
        if (!m_server_env->is_valid()) {
            std::cerr << "Error: Invalid server environment" << std::endl;
            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_name = service_name;
    return true;
}

// Helper method implementations
std::string server_service::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();
}

std::string server_service::get_service_dir() const {
    return m_server_env->get_DROPSHELL_DIR() + "/" + m_service_name;
}

std::string server_service::get_env_path() const {
    return get_service_dir() + "/" + m_service_name + ".env";
}

std::string server_service::get_script_dir() const {
    return get_service_dir() + "/template";
}

bool server_service::check_service_dir_exists(const std::string& ssh_cmd) const {
    std::string check_dir_cmd = ssh_cmd + "'test -d " + get_service_dir() + "'";
    if (system(check_dir_cmd.c_str()) != 0) {
        std::cerr << "Error: Service directory not found on server - has it been installed?" << std::endl;
        return false;
    }
    return true;
}

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

bool server_service::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 server_service::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 server_service::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 server_service::install() {
    maketitle("Installing " + m_service_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 info;
    if (!tm.get_template_info(m_service_name, info)) {
        std::cerr << "Error: Template '" << m_service_name << "' not found" << std::endl;
        return false;
    }
    
    std::string script_dir = get_script_dir();
    
    // Create service directory
    std::string mkdir_cmd = "'mkdir -p " + script_dir + "'";
    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 " << info.path << " to " << script_dir << "/" << std::endl;
    std::string rsync_cmd = "rsync --delete -zrpc -e 'ssh -p " + m_server_env->get_SSH_PORT() + "' " + 
                        info.path + "/ " + 
                        m_server_env->get_SSH_USER() + "@" + m_server_env->get_SSH_HOST() + ":" + 
                        script_dir + "/";
    execute_local_command(rsync_cmd,"Failed to copy template files");
    
    // Copy service env file
    std::string user_dir;
    if (!get_user_directory(user_dir)) {
        std::cerr << "Error: User directory not set" << std::endl;
        return false;
    }
    fs::path service_env = fs::path(user_dir) / "servers" / m_server_name / (m_service_name + ".env");
    std::string scp_cmd = "scp -P " + m_server_env->get_SSH_PORT() + " " + 
                         service_env.string() + " " + 
                         m_server_env->get_SSH_USER() + "@" + m_server_env->get_SSH_HOST() + ":" + 
                         get_env_path();
    if (!execute_ssh_command(scp_cmd, "Failed to copy service environment file")) {
        return false;
    }
    
    // Run install script
    std::string install_cmd = "'cd " + script_dir + 
                            " && /bin/bash _install.sh " + get_env_path() + "'";
    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 server_service::run_command(const std::string& command) {
    if (!m_server_env) {
        std::cerr << "Error: Server service not initialized" << std::endl;
        return false;
    }
    
    std::string ssh_cmd = construct_ssh_cmd();
    std::string script_dir = get_script_dir();
    std::string script_path = script_dir + "/" + command + ".sh";
    
    // Check if service directory exists
    if (!check_service_dir_exists(ssh_cmd)) {
        return false;
    }
    
    // Check if command script exists
    if (!check_remote_file_exists(ssh_cmd, script_path)) {
        return false;
    }
    
    // Check if env file exists
    if (!check_remote_file_exists(ssh_cmd, get_env_path())) {
        return false;
    }
    
    // Run the command
    std::string run_cmd = "'cd " + script_dir + 
                         " && /bin/bash " + script_path + " " + get_env_path() + "'";
    return execute_ssh_command(run_cmd, "Command returned error code: " + script_path);
}

bool server_service::backup() {
    maketitle("Backing up " + m_service_name + " on " + m_server_name);

    if (!m_server_env) {
        std::cerr << "Error: Server service not initialized" << std::endl;
        return false;
    }
    
    std::string ssh_cmd = construct_ssh_cmd();
    std::string script_dir = get_script_dir();
    std::string script_path = script_dir + "/_backup.sh";
    
    // Check if basic installed stuff is in place.
    if (!check_service_dir_exists(ssh_cmd) || !check_remote_file_exists(ssh_cmd, script_path) || !check_remote_file_exists(ssh_cmd, get_env_path()))
        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 user_dir;
    if (!get_user_directory(user_dir)) {
        std::cerr << "Error: User directory not set" << std::endl;
        return false;
    }
    fs::path local_backups_dir = fs::path(user_dir) / "backups";
    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_name + "-" + datetime.str() + ".tgz";
    std::string server_backup_path = server_backups_dir + "/" + backup_filename;
    std::string local_backup_path = (local_backups_dir / backup_filename).string();

    // Run backup script
    std::string backup_cmd = "'cd " + script_dir + 
                           " && /bin/bash \""+script_path+"\" " + get_env_path() + " " + 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;
}

bool server_service::is_healthy()
{
    if (!m_server_env) {
        std::cerr << "Error: Server service not initialized" << std::endl;
        return false;
    }
    
    std::string service_dir = m_server_env->get_DROPSHELL_DIR() + "/" + m_service_name;
    std::string script_dir = service_dir + "/template";
    std::string env_path = service_dir + "/" + m_service_name + ".env"; 

    // Run status script, does not display output.
    return execute_ssh_command("'cd " + script_dir + " && /bin/bash _status.sh " + env_path + " > /dev/null 2>&1'","");
}

std::string server_service::healthtick()
{
    std::string green_tick = "\033[32m✓\033[0m";
    std::string red_cross = "\033[31m✗\033[0m";    
    
    if (is_healthy())
        return green_tick;
    else
        return red_cross;
}

} // namespace dropshell