using Google.Protobuf.WellKnownTypes;
using Mapster;
using AppTx = CMSMicroservice.Application.TransactionsCQ.Queries.GetAllTransactionsByFilter;
using AppTxSummary = CMSMicroservice.Application.TransactionsCQ.Queries.GetTransactionSummary;
using ProtoTx = CMSMicroservice.Protobuf.Protos.Transactions;

namespace CMSMicroservice.WebApi.Common.Mappings;

public class TransactionsProfile : IRegister
{
    public void Register(TypeAdapterConfig config)
    {
        config.NewConfig<ProtoTx.GetAllTransactionsByFilterRequest, AppTx.GetAllTransactionsByFilterQuery>()
            .Map(dest => dest.PaginationState, src => src.PaginationState)
            .Map(dest => dest.SortBy, src => src.SortBy)
            .Map(dest => dest.Filter, src => src.Filter);

        config.NewConfig<ProtoTx.GetAllTransactionsByFilterFilter, AppTx.GetAllTransactionsByFilterFilter>()
            .Map(dest => dest.Id, src => src.Id)
            .Map(dest => dest.Amount, src => src.Amount)
            .Map(dest => dest.Description, src => src.Description)
            .Map(dest => dest.PaymentStatus, src => src.HasPaymentStatus ? (Domain.Enums.PaymentStatus?)src.PaymentStatus : null)
            .Map(dest => dest.PaymentDate, src => src.PaymentDate != null ? src.PaymentDate.ToDateTime() : (DateTime?)null)
            .Map(dest => dest.RefId, src => src.RefId)
            .Map(dest => dest.Type, src => src.HasType ? (Domain.Enums.TransactionType?)src.Type : null)
            .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)
            .Map(dest => dest.PaymentDateFrom, src => src.PaymentDateFrom != null ? src.PaymentDateFrom.ToDateTime() : (DateTime?)null)
            .Map(dest => dest.PaymentDateTo, src => src.PaymentDateTo != null ? src.PaymentDateTo.ToDateTime() : (DateTime?)null);

        config.NewConfig<AppTx.GetAllTransactionsByFilterResponseDto, ProtoTx.GetAllTransactionsByFilterResponse>()
            .Map(dest => dest.MetaData, src => src.MetaData)
            .Map(dest => dest.Models, src => src.Models);

        config.NewConfig<AppTx.GetAllTransactionsByFilterResponseModel, ProtoTx.GetAllTransactionsByFilterResponseModel>()
            .Map(dest => dest.Id, src => src.Id)
            .Map(dest => dest.Amount, src => src.Amount)
            .Map(dest => dest.Description, src => src.Description)
            .Map(dest => dest.PaymentStatus, src => src.PaymentStatus)
            .Map(dest => dest.PaymentDate, src => src.PaymentDate.HasValue ? Timestamp.FromDateTime(DateTime.SpecifyKind(src.PaymentDate.Value, DateTimeKind.Utc)) : null)
            .Map(dest => dest.RefId, src => src.RefId)
            .Map(dest => dest.Type, src => src.Type)
            .Map(dest => dest.Created, src => Timestamp.FromDateTime(DateTime.SpecifyKind(src.Created, DateTimeKind.Utc)));

        config.NewConfig<ProtoTx.GetTransactionSummaryRequest, AppTxSummary.GetTransactionSummaryQuery>()
            .Map(dest => dest.Filter, src => src.Filter);

        config.NewConfig<AppTxSummary.GetTransactionSummaryResponseDto, ProtoTx.GetTransactionSummaryResponse>()
            .Map(dest => dest.TotalCount, src => src.TotalCount)
            .Map(dest => dest.TotalAmount, src => src.TotalAmount)
            .Map(dest => dest.SuccessCount, src => src.SuccessCount)
            .Map(dest => dest.PendingCount, src => src.PendingCount)
            .Map(dest => dest.RejectCount, src => src.RejectCount);
    }
}
