using CliWrap;
using InnovEnergy.Lib.Utils;

namespace InnovEnergy.App.RemoteSupportConsole;

public static class Ssh
{
    const String ConfigFile = "/data/innovenergy/openvpn/installation-name";
    
    public static Task<Int32> Interactive(String host, String? installationName, String user, String port)
    {
        var fixInstallationName = $"echo '{installationName}' > {ConfigFile}; exec bash -l";
        
        var args = GetArgs(host, user, port)
                  .Append(fixInstallationName);
        
        return Cli
              .Wrap("ssh")
              .WithArguments(args, true)
              .ExecuteInteractive();
    }

    public static Command Command(String host, String user , String port)
    {
        var args = GetArgs(host, user, port);

        return Cli
              .Wrap("ssh")
              .WithArguments(args, true);
    }

    private static IEnumerable<String> GetArgs(String host, String user, String port)
    {
        return new[]
        {
            "-o UserKnownHostsFile=/dev/null",
            "-o StrictHostKeyChecking=no",
            "-o ServerAliveInterval=5",
            "-o PasswordAuthentication=no",
            "-o LogLevel=ERROR",
            "-tt", // allocate PTS, necessary
            $"-p {port}",
            $"{user}@{host}"
        };
    }
}