using CMSMicroservice.Application.Common.Exceptions;
using CMSMicroservice.Application.Common.Interfaces;
using CMSMicroservice.Domain.Entities;
using Mapster;

namespace CMSMicroservice.Application.PackageCQ.Queries.GetCustomerPackageDetails;

public class GetCustomerPackageDetailsQueryHandler : IRequestHandler<GetCustomerPackageDetailsQuery, GetCustomerPackageDetailsResponseDto>
{
    private readonly IApplicationDbContext _context;

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

    public async Task<GetCustomerPackageDetailsResponseDto> Handle(GetCustomerPackageDetailsQuery request, CancellationToken cancellationToken)
    {
        var package = await _context.Packages
            .AsNoTracking()
            .Include(p => p.PackageFeatures)
                .ThenInclude(pf => pf.ClubFeature)
            .FirstOrDefaultAsync(x => x.Id == request.PackageId && !x.IsDeleted, cancellationToken);

        if (package == null)
            throw new NotFoundException(nameof(Package), request.PackageId);

        var response = new GetCustomerPackageDetailsResponseDto
        {
            Id = package.Id,
            Title = package.Title,
            Description = package.Description,
            Price = package.Price,
            ImagePath = package.ImagePath,
            ActivationFee = package.ActivationFee,
            DiscountMultiplier = (double)package.DiscountMultiplier,
            MagicWalletMultiplier = (double)package.MagicWalletMultiplier,
            MagicWalletMaxDeposit = package.MagicWalletMaxDeposit,
            MagicWalletMaxCredit = package.MagicWalletMaxCredit,
            IsBasePackage = package.IsBasePackage,
            SupportsDayaPurchase = package.SupportsDayaPurchase,
            SupportsDirectPurchase = package.SupportsDirectPurchase
        };

        // بارگذاری ویژگی‌ها از DB (PackageFeatures → ClubFeature)
        if (package.PackageFeatures?.Any() == true)
        {
            response.Features = package.PackageFeatures
                .Where(pf => pf.ClubFeature != null)
                .Select(pf => new PackageFeatureDto
                {
                    Title = pf.ClubFeature.Title ?? string.Empty,
                    Description = pf.ClubFeature.Description ?? string.Empty,
                    Icon = "feature",
                    IsHighlighted = pf.ClubFeature.IsActive
                })
                .ToList();
        }
        else
        {
            // فال‌بک به ویژگی‌های پیش‌فرض
            response.Features = new List<PackageFeatureDto>
            {
                new() { Title = "درآمد کمیسیون", Description = "دریافت کمیسیون از فروش محصولات", Icon = "commission", IsHighlighted = true },
                new() { Title = "پشتیبانی 24/7", Description = "دسترسی به پشتیبانی در تمام ساعات شبانه روز", Icon = "support", IsHighlighted = false },
                new() { Title = "آموزش‌های تخصصی", Description = "دسترسی به دوره‌های آموزشی و وبینارها", Icon = "education", IsHighlighted = true }
            };
        }

        // شرایط خرید
        response.Requirements = new PurchaseRequirementsDto
        {
            RequiresMembership = false,
            MinimumWalletBalance = package.Price / 10,
            Restrictions = new List<string>
            {
                "باید حداقل 18 سال سن داشته باشید",
                "تایید هویت الزامی است"
            }
        };

        return response;
    }
}
