// EN: Handler for GetSessionQuery.
// VI: Handler cho GetSessionQuery.

using MediatR;
using FnbEngine.Domain.AggregatesModel.SessionAggregate;

namespace FnbEngine.API.Application.Queries;

/// <summary>
/// EN: Handler for getting session details.
/// VI: Handler lấy thông tin phiên.
/// </summary>
public class GetSessionQueryHandler : IRequestHandler<GetSessionQuery, SessionDto?>
{
    private readonly ISessionRepository _sessionRepository;

    public GetSessionQueryHandler(ISessionRepository sessionRepository)
    {
        _sessionRepository = sessionRepository ?? throw new ArgumentNullException(nameof(sessionRepository));
    }

    public async Task<SessionDto?> Handle(GetSessionQuery request, CancellationToken cancellationToken)
    {
        var session = await _sessionRepository.GetByIdAsync(request.SessionId, cancellationToken);
        
        if (session == null)
            return null;

        return new SessionDto(
            session.Id,
            session.TableId,
            session.ShopId,
            session.GuestCount,
            session.StartedAt,
            session.ClosedAt,
            session.Status
        );
    }
}
