package test.it;

import java.io.File;
import java.util.Enumeration;
import java.util.Objects;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

public class IT_DeployRestartNoCleanNoTestsMain {

    // ====== НАСТРОЙКИ (можно переопределять systemProperty) ======
    private static final String REMOTE_HOST = System.getProperty("it.remoteHost", "shineup.me");
    private static final String REMOTE_USER = System.getProperty("it.remoteUser", "player");

    private static final String REMOTE_DIR = System.getProperty("it.remoteDir", "/home/player/SHiNE/shine-server");
    private static final String REMOTE_JAR = REMOTE_DIR + "/shine-server.jar";

    private static final String SERVICE_NAME = System.getProperty("it.service", "shine-server");
    private static final String LOCAL_JAR = System.getProperty("it.localJar", "build/libs/shine-server.jar");

    public static void main(String[] args) {
        ensureSudoNoPasswordOrThrow();

        // 1) stop service на сервере
        sshStrict("sudo systemctl stop " + SERVICE_NAME + " || true");

        // 2) upload jar -> .new
        validateLocalFatJarOrThrow(LOCAL_JAR);
        sshStrict("mkdir -p " + q(REMOTE_DIR));
        scpStrict(LOCAL_JAR, REMOTE_JAR + ".new");
        verifyRemoteNewJarOrThrow(REMOTE_JAR + ".new");

        // 3) заменить jar атомарно
        sshStrict("mv -f " + q(REMOTE_JAR + ".new") + " " + q(REMOTE_JAR));

        // 4) start service
        sshStrict("sudo systemctl start " + SERVICE_NAME);

        // 5) дождаться поднятия
        waitRemotePort7070();

        System.out.println("deploy_no_clean_no_tests_done");
    }

    private static void ensureSudoNoPasswordOrThrow() {
        // Проверяем именно возможность sudo без пароля.
        // systemctl status может возвращать non-zero для inactive/failed сервиса,
        // и это не должно считаться проблемой прав доступа.
        int code = ssh("sudo -n true");
        if (code == 0) return;
        throw new RuntimeException(
                "Remote sudo requires password for " + REMOTE_USER + "@" + REMOTE_HOST
                        + ". Configure NOPASSWD for 'systemctl ... " + SERVICE_NAME + "' or run deploy with privileged user."
        );
    }

    private static void waitRemotePort7070() {
        for (int i = 0; i < 50; i++) {
            int code = ssh("ss -ltnp | grep -q ':7070'");
            if (code == 0) return;
            sleepMs(200);
        }
        throw new RuntimeException("Remote port 7070 did not start in time on " + REMOTE_HOST);
    }

    private static void sshStrict(String remoteCmd) {
        int code = ssh(remoteCmd);
        if (code != 0) throw new RuntimeException("SSH command failed (" + code + "): " + remoteCmd);
    }

    private static int ssh(String remoteCmd) {
        String cmd = "ssh " + REMOTE_USER + "@" + REMOTE_HOST + " " + q("bash -lc " + q(remoteCmd));
        return sh(cmd);
    }

    private static void scpStrict(String local, String remote) {
        Objects.requireNonNull(local);
        Objects.requireNonNull(remote);
        int code = sh("scp -p " + q(local) + " " + REMOTE_USER + "@" + REMOTE_HOST + ":" + q(remote));
        if (code != 0) throw new RuntimeException("SCP failed (" + code + ")");
    }

    private static int sh(String cmd) {
        try {
            Process p = new ProcessBuilder("bash", "-lc", cmd).inheritIO().start();
            return p.waitFor();
        } catch (Exception e) {
            throw new RuntimeException("Command error: " + cmd, e);
        }
    }

    private static String q(String s) {
        return "'" + s.replace("'", "'\"'\"'") + "'";
    }

    private static void sleepMs(long ms) {
        try {
            Thread.sleep(ms);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
    }

    private static void validateLocalFatJarOrThrow(String localJarPath) {
        File jar = new File(localJarPath);
        if (!jar.isFile()) {
            throw new RuntimeException("Local jar not found: " + localJarPath);
        }
        long size = jar.length();
        if (size < 10L * 1024L * 1024L) {
            throw new RuntimeException("Local jar is too small for fat-jar: " + size + " bytes (" + localJarPath + ")");
        }
        try (JarFile jf = new JarFile(jar)) {
            boolean hasJetty = false;
            boolean hasBc = false;
            Enumeration<JarEntry> entries = jf.entries();
            while (entries.hasMoreElements()) {
                String name = entries.nextElement().getName();
                if (!hasJetty && "org/eclipse/jetty/server/Handler.class".equals(name)) hasJetty = true;
                if (!hasBc && "org/bouncycastle/jce/provider/BouncyCastleProvider.class".equals(name)) hasBc = true;
                if (hasJetty && hasBc) break;
            }
            if (!hasJetty || !hasBc) {
                throw new RuntimeException(
                        "Local jar doesn't look like fat-jar (missing deps). hasJetty=" + hasJetty + ", hasBC=" + hasBc
                );
            }
        } catch (Exception e) {
            throw new RuntimeException("Failed to inspect local jar: " + localJarPath, e);
        }
    }

    private static void verifyRemoteNewJarOrThrow(String remoteJarNewPath) {
        String cmd = "test -f " + q(remoteJarNewPath) + " && " +
                "sz=$(stat -c %s " + q(remoteJarNewPath) + ") && " +
                "echo remote_new_size=$sz && test \"$sz\" -ge 10485760";
        int code = ssh(cmd);
        if (code != 0) {
            throw new RuntimeException("Remote uploaded jar is missing or too small: " + remoteJarNewPath);
        }
    }
}
