using Lada.Framework.Data.Domains.Cms;
using Lada.Framework.Data.Repositories;
using Lada.Framework.DTO;

namespace Ladaer.BackEnd.Infrastructures.Services.Cms
{
    public interface IProductCategoryService
    {
        Task<BaseResult> RemoveProductCategoryByProductIdAsync(int productId);
    }
    public class ProductCategoryService : IProductCategoryService
    {
        private readonly IRepository<ProductCategory> _productCategoryRepository;

        public ProductCategoryService(IRepository<ProductCategory> productCategoryRepository)
        {
            _productCategoryRepository = productCategoryRepository;
        }

        public async Task<BaseResult> RemoveProductCategoryByProductIdAsync(int productId)
        {
            var result = new BaseResult();
            var productCategories = _productCategoryRepository.Query().Where(pt => pt.ProductId == productId).ToList();
            try
            {
                if (productCategories != null && productCategories.Any())
                {
                    foreach (var productCategory in productCategories)
                    {
                        await _productCategoryRepository.RemoveAsync(productCategory);
                    }
                }
            }
            catch (Exception ex)
            {
                result.Result = Result.Failed;
                result.Message = ex.Message;
            }

            return result;
        }
    }
}
