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

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

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