using CMSMicroservice.Application.ClubMembershipCQ.Queries.GetAllClubMemberships;
using CMSMicroservice.Application.ClubMembershipCQ.Queries.GetClubMembership;
using CMSMicroservice.Application.ClubMembershipCQ.Queries.GetClubStatistics;
using CMSMicroservice.Protobuf.Protos.ClubMembership;
using Google.Protobuf.WellKnownTypes;
using Mapster;
using System.Linq;

namespace CMSMicroservice.WebApi.Common.Mappings;

public class ClubMembershipProfile : IRegister
{
    public void Register(TypeAdapterConfig config)
    {
        // =====================
        // GetClubMembership
        // =====================
        
        // GetClubMembershipRequest -> GetClubMembershipQuery
        config.NewConfig<GetClubMembershipRequest, GetClubMembershipQuery>()
            .MapWith(src => new GetClubMembershipQuery
            {
                UserId = src.UserId
            });

        // ClubMembershipDto -> GetClubMembershipResponse
        config.NewConfig<ClubMembershipDto, GetClubMembershipResponse>()
            .MapWith(src => new GetClubMembershipResponse
            {
                Id = src.Id,
                UserId = src.UserId,
                PackageId = 0, // Not available
                PackageName = string.Empty, // Not available
                ActivationCode = string.Empty, // Not available
                ActivatedAt = src.ActivatedAt.HasValue 
                    ? Timestamp.FromDateTime(DateTime.SpecifyKind(src.ActivatedAt.Value, DateTimeKind.Utc)) 
                    : null,
                ExpiresAt = null, // Not available
                IsActive = src.IsActive,
                Created = Timestamp.FromDateTimeOffset(src.Created),
                Features = { }, // Empty for now
                Status = src.IsActive ? "Active" : "Inactive",
                DaysRemaining = 0 // Calculate if ExpiresAt is available
            });

        // =====================
        // GetAllClubMemberships
        // =====================
        
        // GetAllClubMembershipsRequest -> GetAllClubMembershipsQuery
        config.NewConfig<GetAllClubMembershipsRequest, GetAllClubMembershipsQuery>()
            .MapWith(src => new GetAllClubMembershipsQuery
            {
                PaginationState = new Application.Common.Models.PaginationState
                {
                    PageNumber = src.PageIndex > 0 ? src.PageIndex : 1,
                    PageSize = src.PageSize > 0 ? src.PageSize : 20
                },
                Filter = new GetAllClubMembershipsFilter
                {
                    UserId = src.UserId,
                    IsActive = src.IsActive
                }
            });

        // GetAllClubMembershipsResponseDto -> GetAllClubMembershipsResponse
        config.NewConfig<GetAllClubMembershipsResponseDto, GetAllClubMembershipsResponse>()
            .MapWith(src => new GetAllClubMembershipsResponse
            {
                MetaData = new CMSMicroservice.Protobuf.Protos.MetaData
                {
                    CurrentPage = src.MetaData.CurrentPage,
                    PageSize = src.MetaData.PageSize,
                    TotalCount = src.MetaData.TotalCount,
                    TotalPage = src.MetaData.TotalPage
                },
                Models = { src.Models.Select(MapToModel) }
            });

        // =====================
        // GetClubStatistics
        // =====================

        // GetClubStatisticsRequest -> GetClubStatisticsQuery
        config.NewConfig<GetClubStatisticsRequest, GetClubStatisticsQuery>()
            .MapWith(src => new GetClubStatisticsQuery());

        // GetClubStatisticsResponseDto -> GetClubStatisticsResponse
        config.NewConfig<GetClubStatisticsResponseDto, GetClubStatisticsResponse>()
            .MapWith(src => new GetClubStatisticsResponse
            {
                TotalMembers = src.TotalMembers,
                ActiveMembers = src.ActiveMembers,
                InactiveMembers = src.InactiveMembers,
                ExpiredMembers = src.ExpiredMembers,
                ActivePercentage = src.ActivePercentage,
                TotalRevenue = src.TotalRevenue,
                AverageMembershipDurationDays = src.AverageMembershipDurationDays,
                ExpiringSoonCount = src.ExpiringSoonCount,
                PackageDistribution = { src.PackageDistribution.Select(p => new PackageLevelDistribution
                {
                    PackageId = p.PackageId,
                    PackageName = p.PackageName ?? string.Empty,
                    MemberCount = p.MemberCount,
                    Percentage = p.Percentage
                }) },
                MonthlyTrend = { src.MonthlyTrend.Select(t => new MonthlyMembershipTrend
                {
                    Month = t.Month ?? string.Empty,
                    Activations = t.Activations,
                    Expirations = t.Expirations,
                    NetChange = t.NetChange
                }) }
            });
    }

    private static ClubMembershipModel MapToModel(GetAllClubMembershipsResponseModel src)
    {
        return new ClubMembershipModel
        {
            Id = src.Id,
            UserId = src.UserId,
            UserName = src.UserName ?? string.Empty,
            PackageId = 0, // Not available in current entity
            PackageName = string.Empty, // Not available in current entity
            ActivationCode = string.Empty, // Not available in current entity
            ActivatedAt = src.ActivatedAt.HasValue 
                ? Timestamp.FromDateTime(DateTime.SpecifyKind(src.ActivatedAt.Value, DateTimeKind.Utc)) 
                : null,
            ExpiresAt = null, // Not available in current entity
            IsActive = src.IsActive,
            IsExpired = false, // Calculate based on ExpiresAt if available
            Created = Timestamp.FromDateTime(src.Created.UtcDateTime)
        };
    }
}
