using Google.Protobuf.WellKnownTypes;
using CMSMicroservice.Domain.Enums;
using Mapster;
using AppCycle = CMSMicroservice.Application.ClubMembershipCycleCQ.Queries.GetAllClubMembershipCycleByFilter;
using AppCycleSummary = CMSMicroservice.Application.ClubMembershipCycleCQ.Queries.GetClubMembershipCycleSummary;
using ProtoCycle = CMSMicroservice.Protobuf.Protos.ClubMembershipCycle;

namespace CMSMicroservice.WebApi.Common.Mappings;

public class ClubMembershipCycleProfile : IRegister
{
    public void Register(TypeAdapterConfig config)
    {
        config.NewConfig<ProtoCycle.GetAllClubMembershipCycleByFilterRequest, AppCycle.GetAllClubMembershipCycleByFilterQuery>()
            .Map(dest => dest.PaginationState, src => src.PaginationState)
            .Map(dest => dest.SortBy, src => src.SortBy)
            .Map(dest => dest.Filter, src => src.Filter);

        config.NewConfig<ProtoCycle.GetAllClubMembershipCycleByFilterFilter, AppCycle.GetAllClubMembershipCycleByFilterFilter>()
            .Map(dest => dest.UserId, src => src.UserId)
            .Map(dest => dest.IsCurrentCycle, src => src.IsCurrentCycle)
            .Map(dest => dest.PackageId, src => src.PackageId)
            .Map(dest => dest.CycleNumber, src => src.CycleNumber)
            .Map(dest => dest.PurchaseMethod, src => src.PurchaseMethod == null ? null : (PackagePurchaseMethod?)src.PurchaseMethod)
            .Map(dest => dest.MagicState, src => src.MagicState)
            .Map(dest => dest.PackagePurchasedFrom, src => src.PackagePurchasedFrom != null ? src.PackagePurchasedFrom.ToDateTime() : (DateTime?)null)
            .Map(dest => dest.PackagePurchasedTo, src => src.PackagePurchasedTo != null ? src.PackagePurchasedTo.ToDateTime() : (DateTime?)null);

        config.NewConfig<AppCycle.GetAllClubMembershipCycleByFilterResponseDto, ProtoCycle.GetAllClubMembershipCycleByFilterResponse>()
            .Map(dest => dest.MetaData, src => src.MetaData)
            .Map(dest => dest.Models, src => src.Models);

        config.NewConfig<AppCycle.GetAllClubMembershipCycleByFilterResponseModel, ProtoCycle.ClubMembershipCycleModel>()
            .Map(dest => dest.Id, src => src.Id)
            .Map(dest => dest.UserId, src => src.UserId)
            .Map(dest => dest.UserName, src => src.UserName)
            .Map(dest => dest.ClubMembershipId, src => src.ClubMembershipId)
            .Map(dest => dest.CycleNumber, src => src.CycleNumber)
            .Map(dest => dest.PackageId, src => src.PackageId)
            .Map(dest => dest.PackageName, src => src.PackageName)
            .Map(dest => dest.PackageAmount, src => src.PackageAmount)
            .Map(dest => dest.PurchaseMethod, src => (int)src.PurchaseMethod)
            .Map(dest => dest.PackagePurchasedAt, src => Timestamp.FromDateTime(DateTime.SpecifyKind(src.PackagePurchasedAt, DateTimeKind.Utc)))
            .Map(dest => dest.MagicStartedAt, src => src.MagicStartedAt)
            .Map(dest => dest.MagicCompletedAt, src => src.MagicCompletedAt)
            .Map(dest => dest.IsCurrentCycle, src => src.IsCurrentCycle)
            .Map(dest => dest.CreatedAt, src => Timestamp.FromDateTimeOffset(src.Created));

        config.NewConfig<ProtoCycle.GetClubMembershipCycleSummaryRequest, AppCycleSummary.GetClubMembershipCycleSummaryQuery>()
            .Map(dest => dest.Filter, src => src.Filter);

        config.NewConfig<AppCycleSummary.GetClubMembershipCycleSummaryResponseDto, ProtoCycle.GetClubMembershipCycleSummaryResponse>()
            .Map(dest => dest.TotalCount, src => src.TotalCount)
            .Map(dest => dest.CurrentCycleCount, src => src.CurrentCycleCount)
            .Map(dest => dest.MagicActiveCount, src => src.MagicActiveCount);
    }
}
