using CMSMicroservice.Application.UserOrderCQ.Commands.UpdateOrderStatus;
using CMSMicroservice.Application.UserOrderCQ.Commands.ApplyDiscountToOrder;
using CMSMicroservice.Application.UserOrderCQ.Queries.GetOrdersByDateRange;
using CMSMicroservice.Application.UserOrderCQ.Queries.CalculateOrderPV;
using Google.Protobuf.WellKnownTypes;

namespace CMSMicroservice.WebApi.Common.Mappings;

public class UserOrderProfile : IRegister
{
    void IRegister.Register(TypeAdapterConfig config)
    {
        config.NewConfig<Protobuf.Protos.UserOrder.GetAllUserOrderByFilterRequest, Application.UserOrderCQ.Queries.GetAllUserOrderByFilter.GetAllUserOrderByFilterQuery>()
            .IgnoreIf((src, dest) => src.Filter == null || !src.Filter.HasPaymentStatus, dest => dest.Filter.PaymentStatus)
            .IgnoreIf((src, dest) => src.Filter == null || !src.Filter.HasPaymentMethod, dest => dest.Filter.PaymentMethod)
            .IgnoreIf((src, dest) => src.Filter == null || !src.Filter.HasDeliveryStatus, dest => dest.Filter.DeliveryStatus);

        // UpdateOrderStatus
        config.NewConfig<Protobuf.Protos.UserOrder.UpdateOrderStatusRequest, UpdateOrderStatusCommand>();
        config.NewConfig<UpdateOrderStatusResponseDto, Protobuf.Protos.UserOrder.UpdateOrderStatusResponse>();

        // GetOrdersByDateRange
        config.NewConfig<Protobuf.Protos.UserOrder.GetOrdersByDateRangeRequest, GetOrdersByDateRangeQuery>()
            .Map(dest => dest.StartDate, src => src.StartDate.ToDateTime())
            .Map(dest => dest.EndDate, src => src.EndDate.ToDateTime())
            .Map(dest => dest.Status, src => src.Status != null ? (int?)src.Status.Value : null)
            .Map(dest => dest.UserId, src => src.UserId != null ? (long?)src.UserId.Value : null);
        
        config.NewConfig<GetOrdersByDateRangeResponseDto, Protobuf.Protos.UserOrder.GetOrdersByDateRangeResponse>()
            .Map(dest => dest.Orders, src => src.Orders);
        
        config.NewConfig<OrderSummaryDto, Protobuf.Protos.UserOrder.OrderSummaryDto>()
            .Map(dest => dest.CreatedAt, src => Timestamp.FromDateTime(src.Created.ToUniversalTime()));

        // ApplyDiscountToOrder
        config.NewConfig<Protobuf.Protos.UserOrder.ApplyDiscountToOrderRequest, ApplyDiscountToOrderCommand>();
        config.NewConfig<ApplyDiscountToOrderResponseDto, Protobuf.Protos.UserOrder.ApplyDiscountToOrderResponse>();

        // CalculateOrderPV
        config.NewConfig<Protobuf.Protos.UserOrder.CalculateOrderPVRequest, CalculateOrderPVQuery>();
        config.NewConfig<CalculateOrderPVResponseDto, Protobuf.Protos.UserOrder.CalculateOrderPVResponse>();
        config.NewConfig<ProductPVDto, Protobuf.Protos.UserOrder.ProductPVDto>();
    }
}
