using CashLineERP.Contracts.Integration;

namespace CashLineERP.Application.Integration;

public sealed class DeadLetterService : IDeadLetterService
{
    private readonly IDeadLetterRepository _repository;

    public DeadLetterService(IDeadLetterRepository repository)
    {
        _repository = repository;
    }

    public IReadOnlyCollection<DeadLetterQueueItemResponse> GetDeadLetters(string? queueType = null) =>
        _repository.GetDeadLetters(queueType)
            .Select(Map)
            .ToArray();

    private static DeadLetterQueueItemResponse Map(DeadLetterQueueItemView item) =>
        new(
            item.QueueType,
            item.ItemId,
            item.ReferenceNumber,
            item.ContextCode,
            item.EntryType,
            item.CorrelationKey,
            item.StatusCode,
            item.AttemptNumber,
            item.AvailableAtUtc,
            item.LastErrorText,
            item.CreatedAtUtc);
}
