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

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

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