using Google.Protobuf.WellKnownTypes;
using CMSMicroservice.Domain.Enums;
using Mapster;
using AppHistory = CMSMicroservice.Application.ClubMembershipCycleHistoryCQ.Queries.GetAllClubMembershipCycleHistoryByFilter;
using ProtoHistory = CMSMicroservice.Protobuf.Protos.ClubMembershipCycleHistory;

namespace CMSMicroservice.WebApi.Common.Mappings;

public class ClubMembershipCycleHistoryProfile : IRegister
{
    public void Register(TypeAdapterConfig config)
    {
        config.NewConfig<ProtoHistory.GetAllClubMembershipCycleHistoryByFilterRequest, AppHistory.GetAllClubMembershipCycleHistoryByFilterQuery>()
            .Map(dest => dest.PaginationState, src => src.PaginationState)
            .Map(dest => dest.SortBy, src => src.SortBy)
            .Map(dest => dest.Filter, src => src.Filter);

        config.NewConfig<ProtoHistory.GetAllClubMembershipCycleHistoryByFilterFilter, AppHistory.GetAllClubMembershipCycleHistoryByFilterFilter>()
            .Map(dest => dest.UserId, src => src.UserId)
            .Map(dest => dest.ClubMembershipCycleId, src => src.ClubMembershipCycleId)
            .Map(dest => dest.CycleNumber, src => src.CycleNumber)
            .Map(dest => dest.Action, src => src.Action == null ? null : (ClubMembershipCycleAction?)src.Action)
            .Map(dest => dest.CreatedFrom, src => src.CreatedFrom != null ? src.CreatedFrom.ToDateTime() : (DateTime?)null)
            .Map(dest => dest.CreatedTo, src => src.CreatedTo != null ? src.CreatedTo.ToDateTime() : (DateTime?)null);

        config.NewConfig<AppHistory.GetAllClubMembershipCycleHistoryByFilterResponseDto, ProtoHistory.GetAllClubMembershipCycleHistoryByFilterResponse>()
            .Map(dest => dest.MetaData, src => src.MetaData)
            .Map(dest => dest.Models, src => src.Models);

        config.NewConfig<AppHistory.GetAllClubMembershipCycleHistoryByFilterResponseModel, ProtoHistory.ClubMembershipCycleHistoryModel>()
            .Map(dest => dest.Id, src => src.Id)
            .Map(dest => dest.ClubMembershipCycleId, src => src.ClubMembershipCycleId)
            .Map(dest => dest.UserId, src => src.UserId)
            .Map(dest => dest.UserName, src => src.UserName)
            .Map(dest => dest.CycleNumber, src => src.CycleNumber)
            .Map(dest => dest.OldIsCurrentCycle, src => src.OldIsCurrentCycle)
            .Map(dest => dest.NewIsCurrentCycle, src => src.NewIsCurrentCycle)
            .Map(dest => dest.OldMagicStartedAt, src => src.OldMagicStartedAt)
            .Map(dest => dest.NewMagicStartedAt, src => src.NewMagicStartedAt)
            .Map(dest => dest.OldMagicCompletedAt, src => src.OldMagicCompletedAt)
            .Map(dest => dest.NewMagicCompletedAt, src => src.NewMagicCompletedAt)
            .Map(dest => dest.Action, src => (int)src.Action)
            .Map(dest => dest.PerformedBy, src => src.PerformedBy ?? string.Empty)
            .Map(dest => dest.Reason, src => src.Reason ?? string.Empty)
            .Map(dest => dest.CreatedAt, src => Timestamp.FromDateTimeOffset(src.Created));
    }
}
