using MediatR;
using Sledgemapper.Api.Commands;
using Sledgemapper.Api.Infrastructure.Data;
using Sledgemapper.Api.Notifications;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;

namespace Sledgemapper.Api.Handlers
{
    public class DeleteOverlayCommandHandler : BaseCommandHandler<DeleteOverlayCommand, bool>
    {
        public DeleteOverlayCommandHandler(IMediator mediator, SledgemapperDbContext dbcontext) : base(mediator, dbcontext)
        {
        }

        public override async Task<bool> Handle(DeleteOverlayCommand command, CancellationToken cancellationToken)
        {
            await CheckAuthorization(command);
            var jsonString = JsonSerializer.Serialize(command.Overlay);
            var session = await SaveLog(command, "D", "O", jsonString, cancellationToken);
            await Mediator.Publish(new DeleteOverlayNotification(session, command.Overlay, command.UserId), cancellationToken);
            return true;
        }
    }
}
