using CMSMicroservice.Application.TagCQ.Queries.GetProductsByTag;
using CMSMicroservice.Protobuf.Protos.Tag;
using System.Collections.Generic;

namespace CMSMicroservice.WebApi.Common.Mappings;

public class TagProfile : IRegister
{
    void IRegister.Register(TypeAdapterConfig config)
    {
        // GetProductsByTagRequest -> GetProductsByTagQuery
        config.NewConfig<GetProductsByTagRequest, GetProductsByTagQuery>()
            .Map(dest => dest.TagId, src => src.TagId);

        // List<ProductSimpleDto> -> GetProductsByTagResponse
        config.NewConfig<List<ProductSimpleDto>, GetProductsByTagResponse>()
            .MapWith(src => ConvertToResponse(src));
    }

    private static GetProductsByTagResponse ConvertToResponse(List<ProductSimpleDto> products)
    {
        var response = new GetProductsByTagResponse();
        
        foreach (var product in products)
        {
            response.Products.Add(new ProductSimpleModel
            {
                Id = product.Id,
                Title = product.Title,
                Price = product.Price,
                Inventory = product.Inventory,
                IsActive = product.IsActive,
                ImagePath = product.ImagePath
            });
        }

        return response;
    }
}

