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

using BookingService.API.Application.DTOs;
using BookingService.Infrastructure;
using MediatR;
using Microsoft.EntityFrameworkCore;

namespace BookingService.API.Application.Queries;

public class GetResourcesByShopQueryHandler : IRequestHandler<GetResourcesByShopQuery, List<ResourceDto>>
{
    private readonly BookingContext _context;

    public GetResourcesByShopQueryHandler(BookingContext context)
    {
        _context = context ?? throw new ArgumentNullException(nameof(context));
    }

    public async Task<List<ResourceDto>> Handle(GetResourcesByShopQuery request, CancellationToken cancellationToken)
    {
        var query = _context.Resources.Where(r => r.ShopId == request.ShopId);

        if (request.IsActive.HasValue)
        {
            query = query.Where(r => r.IsActive == request.IsActive.Value);
        }

        return await query
            .Select(r => new ResourceDto
            {
                Id = r.Id,
                ShopId = r.ShopId,
                Name = r.Name,
                ResourceType = r.ResourceType,
                Capacity = r.Capacity,
                IsActive = r.IsActive,
                CreatedAt = r.CreatedAt
            })
            .ToListAsync(cancellationToken);
    }
}
