namespace CMSMicroservice.Application.PackageCQ.Queries.GetPackage;
public class GetPackageQueryHandler : IRequestHandler<GetPackageQuery, GetPackageResponseDto>
{
    private readonly IApplicationDbContext _context;

    public GetPackageQueryHandler(IApplicationDbContext context)
    {
        _context = context;
    }

    public async Task<GetPackageResponseDto> Handle(GetPackageQuery request,
        CancellationToken cancellationToken)
    {
        var package = await _context.Packages
            .AsNoTracking()
            .Include(x => x.PackageFeatures)
            .Where(x => x.Id == request.Id)
            .FirstOrDefaultAsync(cancellationToken)
            ?? throw new NotFoundException(nameof(Package), request.Id);

        var response = package.Adapt<GetPackageResponseDto>();
        response.FeatureIds = package.PackageFeatures?
            .Where(pf => pf.IsIncluded)
            .Select(pf => pf.ClubFeatureId)
            .ToList() ?? new();

        return response;
    }
}
