using MediatR;
using Sledgemapper.Api.Commands;
using Sledgemapper.Api.Infrastructure.Data;
using Sledgemapper.Api.Models;
using System;
using System.Threading;
using System.Threading.Tasks;
using Sledgemapper.Api.Core.Entities;

namespace Sledgemapper.Api.Handlers
{
    public class StartNewSessionHandler : BaseCommandHandler<NewSessionCommand, Guid>
    {
 

        public StartNewSessionHandler(IMediator mediator, SledgemapperDbContext dbcontext):base(mediator, dbcontext)
        {
            
        }

        public override async Task<Guid> Handle(NewSessionCommand command, CancellationToken cancellationToken)
        {
            var campaign = await GetCampaignForUser(command);

            var session = new Session
            {
                SessionName = command.SessionName,
                OwnerUserId = command.UserId,
                CampaignId = campaign.CampaignId

            };
            DbContext.Sessions.Add(session);
            await DbContext.SaveChangesAsync(cancellationToken);
            return session.SessionId;
        }
    }
}
