using CMSMicroservice.Application.Common.Interfaces;
using MediatR;
using Microsoft.EntityFrameworkCore;

namespace CMSMicroservice.Application.ClubFeatureCQ.Queries.GetUserClubFeatures;

/// <summary>
/// هندلر برای دریافت لیست ویژگی‌های باشگاه یک کاربر
/// </summary>
public class GetUserClubFeaturesQueryHandler : IRequestHandler<GetUserClubFeaturesQuery, List<UserClubFeatureDto>>
{
    private readonly IApplicationDbContext _context;

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

    public async Task<List<UserClubFeatureDto>> Handle(GetUserClubFeaturesQuery request, CancellationToken cancellationToken)
    {
        var userClubFeatures = await _context.UserClubFeatures
            .Include(ucf => ucf.ClubFeature)
            .Where(ucf => ucf.UserId == request.UserId && !ucf.IsDeleted)
            .Select(ucf => new UserClubFeatureDto
            {
                Id = ucf.Id,
                UserId = ucf.UserId,
                ClubMembershipId = ucf.ClubMembershipId,
                ClubFeatureId = ucf.ClubFeatureId,
                FeatureTitle = ucf.ClubFeature.Title,
                FeatureDescription = ucf.ClubFeature.Description,
                IsActive = ucf.IsActive,
                GrantedAt = ucf.GrantedAt,
                Notes = ucf.Notes,
                SortOrder = ucf.ClubFeature.SortOrder,
                CreatedAt = ucf.Created
            })
            .OrderBy(ucf => ucf.SortOrder)
            .ToListAsync(cancellationToken);

        return userClubFeatures;
    }
}
