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 NewWallCommandHandler : BaseCommandHandler<NewWallCommand, bool>
    {
        public NewWallCommandHandler(IMediator mediator, SledgemapperDbContext dbContext) : base(mediator, dbContext)
        {
        }

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