using CMSMicroservice.Application.Common.Models;
using CMSMicroservice.Application.DiscountShopCQ.Queries.GetDiscountProducts;
using CMSMicroservice.Protobuf.Protos.DiscountProduct;
using Google.Protobuf.WellKnownTypes;
using Mapster;
using ProtoDiscountProductDto = CMSMicroservice.Protobuf.Protos.DiscountProduct.DiscountProductDto;

namespace CMSMicroservice.WebApi.Common.Mappings;

public class DiscountProductProfile : IRegister
{
    void IRegister.Register(TypeAdapterConfig config)
    {
        // GetDiscountProductsRequest (proto) → GetDiscountProductsQuery (CQRS)
        config.NewConfig<GetDiscountProductsRequest, GetDiscountProductsQuery>()
            .Map(dest => dest.SearchTerm,
                src => string.IsNullOrEmpty(src.SearchQuery) ? null : src.SearchQuery)
            .Map(dest => dest.CategoryId,
                src => src.CategoryId != null ? src.CategoryId.Value : (long?)null)
            .Map(dest => dest.IsActive,
                src => src.IsActive != null ? src.IsActive.Value : (bool?)null)
            .Map(dest => dest.InStock,
                src => src.InStock != null ? src.InStock.Value : (bool?)null)
            .Map(dest => dest.MinPrice,
                src => src.MinPrice != null ? (int?)src.MinPrice.Value : null)
            .Map(dest => dest.MaxPrice,
                src => src.MaxPrice != null ? (int?)src.MaxPrice.Value : null)
            .Map(dest => dest.SortBy,
                src => string.IsNullOrEmpty(src.SortBy) ? null : src.SortBy)
            .Map(dest => dest.PaginationQuery, src => new PaginationState
            {
                PageNumber = src.PageNumber > 0 ? src.PageNumber : 1,
                PageSize = src.PageSize > 0 ? src.PageSize : 12
            });

        // GetDiscountProductsResponseDto (CQRS) → GetDiscountProductsResponse (proto)
        config.NewConfig<GetDiscountProductsResponseDto, GetDiscountProductsResponse>()
            .MapWith(src => MapToResponse(src));
    }

    private static GetDiscountProductsResponse MapToResponse(GetDiscountProductsResponseDto src)
    {
        var response = new GetDiscountProductsResponse();

        if (src.MetaData != null)
        {
            response.MetaData = new CMSMicroservice.Protobuf.Protos.MetaData
            {
                TotalCount = src.MetaData.TotalCount,
                PageSize = src.MetaData.PageSize,
                CurrentPage = src.MetaData.CurrentPage,
                TotalPage = src.MetaData.TotalPage
            };
        }

        if (src.Models != null)
        {
            foreach (var p in src.Models)
            {
                response.Models.Add(new ProtoDiscountProductDto
                {
                    Id = p.Id,
                    Title = p.Title ?? string.Empty,
                    ShortInfomation = p.ShortInfomation ?? string.Empty,
                    Price = p.Price,
                    MaxDiscountPercent = p.MaxDiscountPercent,
                    ImagePath = p.ImagePath ?? string.Empty,
                    ThumbnailPath = p.ThumbnailPath ?? string.Empty,
                    RemainingCount = p.RemainingCount,
                    ViewCount = p.ViewCount,
                    SaleCount = p.SaleCount,
                    IsActive = p.IsActive,
                    Created = Timestamp.FromDateTime(DateTime.UtcNow)
                });
            }
        }

        return response;
    }
}
