using System.Diagnostics;

namespace Metin2Launcher.Runtime;

/// <summary>
/// Delivers the runtime key to the game process via environment variables.
/// This is the MVP mechanism — every platform supports it, no native interop,
/// no timing windows. The variables are scoped to the child's environment only
/// (we mutate <see cref="ProcessStartInfo.Environment"/>, not the launcher's
/// global process environment), so other processes on the machine never see them.
/// </summary>
public sealed class EnvVarKeyDelivery : IRuntimeKeyDelivery
{
    public const string MasterKeyVar = "M2PACK_MASTER_KEY_HEX";
    public const string SignPubkeyVar = "M2PACK_SIGN_PUBKEY_HEX";
    public const string KeyIdVar      = "M2PACK_KEY_ID";

    public string Name => "env-var";

    public void Apply(ProcessStartInfo psi, RuntimeKey key)
    {
        if (psi is null) throw new ArgumentNullException(nameof(psi));
        if (key is null) throw new ArgumentNullException(nameof(key));

        // Never set these on the launcher process itself — scope them to the child only.
        psi.Environment[MasterKeyVar] = key.MasterKeyHex;
        psi.Environment[SignPubkeyVar] = key.SignPubkeyHex;
        psi.Environment[KeyIdVar] = key.KeyId;
    }
}
