using JSMR.Application.Circles.Ports;
using JSMR.Application.Common.Caching;

namespace JSMR.Application.Circles.Commands.UpdateCircleStatus;

public class UpdateCircleStatusHandler(ICircleWriter writer, ISpamCircleCache spamCircleCache)
{
    public async Task<UpdateCircleStatusResponse> HandleAsync(UpdateCircleStatusRequest request, CancellationToken cancellationToken = default)
    {
        UpdateCircleStatusResponse response = await writer.UpdateStatusAsync(request, cancellationToken);

        bool wasChangedToSpam = response.OldStatus is not Contracts.CircleStatus.Spam && response.NewStatus is Contracts.CircleStatus.Spam;
        bool wasChangedFromSpam = response.OldStatus is Contracts.CircleStatus.Spam && response.NewStatus is not Contracts.CircleStatus.Spam;

        if (wasChangedToSpam || wasChangedFromSpam)
            await spamCircleCache.RefreshAsync(cancellationToken);

        return response;
    }
}