using System.Collections.Generic;
using System.Linq;

namespace ZB.MOM.WW.MxGateway.Worker.Bootstrap;

public sealed class WorkerBootstrapResult
{
    private WorkerBootstrapResult(
        WorkerExitCode exitCode,
        WorkerOptions? options,
        IReadOnlyList<string> errors)
    {
        ExitCode = exitCode;
        Options = options;
        Errors = errors;
    }

    /// <summary>
    /// Gets the worker process exit code.
    /// </summary>
    public WorkerExitCode ExitCode { get; }

    /// <summary>
    /// Gets the bootstrap options if bootstrap succeeded.
    /// </summary>
    public WorkerOptions? Options { get; }

    /// <summary>
    /// Gets the list of bootstrap errors if any.
    /// </summary>
    public IReadOnlyList<string> Errors { get; }

    /// <summary>
    /// Gets a value indicating whether bootstrap succeeded.
    /// </summary>
    public bool Succeeded => ExitCode == WorkerExitCode.Success;

    /// <summary>
    /// Creates a successful bootstrap result with the given options.
    /// </summary>
    /// <param name="options">Bootstrap options.</param>
    /// <returns>Successful bootstrap result.</returns>
    public static WorkerBootstrapResult Success(WorkerOptions options)
    {
        return new WorkerBootstrapResult(WorkerExitCode.Success, options, []);
    }

    /// <summary>
    /// Creates a failed bootstrap result with the given exit code and errors.
    /// </summary>
    /// <param name="exitCode">Worker exit code.</param>
    /// <param name="errors">Bootstrap errors.</param>
    /// <returns>Failed bootstrap result.</returns>
    public static WorkerBootstrapResult Failure(WorkerExitCode exitCode, IEnumerable<string> errors)
    {
        return new WorkerBootstrapResult(exitCode, null, errors.ToArray());
    }
}
