namespace Baya.Domain.Entities.Payouts;

/// <summary>
/// The allowed-edge table for the <see cref="PayoutBatchStatus"/> machine. A batch opens in
/// <see cref="PayoutBatchStatus.Draft"/>, is submitted (<see cref="PayoutBatchStatus.Processing"/>), then settles
/// to a terminal outcome. <see cref="PayoutBatchStatus.PartiallyFailed"/> is re-enterable: a retry that clears the
/// last failed payout flips it to <see cref="PayoutBatchStatus.Completed"/>.
/// </summary>
public static class PayoutBatchTransitions
{
    private static readonly IReadOnlyDictionary<string, IReadOnlyCollection<string>> Allowed =
        new Dictionary<string, IReadOnlyCollection<string>>
        {
            [PayoutBatchStatus.Draft] = [PayoutBatchStatus.Processing, PayoutBatchStatus.Failed],
            [PayoutBatchStatus.Processing] =
                [PayoutBatchStatus.Completed, PayoutBatchStatus.PartiallyFailed, PayoutBatchStatus.Failed],
            // A retry can clear the last failed payout and complete the batch.
            [PayoutBatchStatus.PartiallyFailed] = [PayoutBatchStatus.Completed, PayoutBatchStatus.PartiallyFailed],
            [PayoutBatchStatus.Completed] = [],
            [PayoutBatchStatus.Failed] = []
        };

    public static bool CanTransition(string from, string to)
        => Allowed.TryGetValue(from, out var targets) && targets.Contains(to);
}
