using System.Collections.Immutable;

namespace StellaOps.Excititor.Connectors.Abstractions;

public sealed class VexConnectorOptionsValidationException : Exception
{
    public VexConnectorOptionsValidationException(
        string connectorId,
        IEnumerable<string> errors)
        : base(BuildMessage(connectorId, errors))
    {
        ConnectorId = connectorId;
        Errors = errors?.ToImmutableArray() ?? ImmutableArray<string>.Empty;
    }

    public string ConnectorId { get; }

    public ImmutableArray<string> Errors { get; }

    private static string BuildMessage(string connectorId, IEnumerable<string> errors)
    {
        var builder = new System.Text.StringBuilder();
        builder.Append("Connector options validation failed for '");
        builder.Append(connectorId);
        builder.Append("'.");

        var list = errors?.ToImmutableArray() ?? ImmutableArray<string>.Empty;
        if (!list.IsDefaultOrEmpty)
        {
            builder.Append(" Errors: ");
            builder.Append(string.Join("; ", list));
        }

        return builder.ToString();
    }
}
