using System.Diagnostics;

namespace ZB.MOM.WW.MxGateway.Server.Workers;

/// <summary>
/// Wraps a System.Diagnostics.Process as an IWorkerProcess.
/// </summary>
internal sealed class SystemWorkerProcess(Process process) : IWorkerProcess
{
    /// <inheritdoc />
    public int Id => process.Id;

    /// <inheritdoc />
    public bool HasExited => process.HasExited;

    /// <inheritdoc />
    public int? ExitCode => process.HasExited ? process.ExitCode : null;

    /// <inheritdoc />
    public async ValueTask WaitForExitAsync(CancellationToken cancellationToken)
    {
        await process.WaitForExitAsync(cancellationToken).ConfigureAwait(false);
    }

    /// <inheritdoc />
    public void Kill(bool entireProcessTree)
    {
        process.Kill(entireProcessTree);
    }

    /// <summary>Disposes the wrapped <see cref="Process"/> instance.</summary>
    public void Dispose()
    {
        process.Dispose();
    }
}
