package com.ecep.contract.ds.project.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.context.annotation.Lazy;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.ecep.contract.IEntityService;
import com.ecep.contract.QueryService;
import com.ecep.contract.ds.project.repository.ProductTypeRepository;
import com.ecep.contract.ds.project.model.ProductType;
import com.ecep.contract.service.VoableService;
import com.ecep.contract.vo.ProductTypeVo;
import com.fasterxml.jackson.databind.JsonNode;

@Lazy
@Service
@CacheConfig(cacheNames = "product-type")
public class ProductTypeService implements IEntityService<ProductType>, QueryService<ProductTypeVo>,
        VoableService<ProductType, ProductTypeVo> {
    @Lazy
    @Autowired
    private ProductTypeRepository productTypeRepository;

    @Override
    public ProductType getById(Integer id) {
        return productTypeRepository.findById(id).orElse(null);
    }

    @Cacheable(key = "#p0")
    @Override
    public ProductTypeVo findById(Integer id) {
        return productTypeRepository.findById(id).map(ProductType::toVo).orElse(null);
    }

    @Cacheable(key = "'name-'+#p0")
    public ProductTypeVo findByName(String name) {
        return productTypeRepository.findByName(name).map(ProductType::toVo).orElse(null);
    }

    @Cacheable(key = "'code-'+#p0")
    public ProductTypeVo findByCode(String code) {
        return productTypeRepository.findByCode(code).map(ProductType::toVo).orElse(null);
    }

    @Cacheable(key = "'all'")
    public List<ProductTypeVo> findAll() {
        return productTypeRepository.findAll().stream().map(ProductType::toVo).toList();
    }

    @Override
    public Page<ProductType> findAll(Specification<ProductType> spec, Pageable pageable) {
        return productTypeRepository.findAll(spec, pageable);
    }

    @Override
    public Page<ProductTypeVo> findAll(JsonNode paramsNode, Pageable pageable) {
        Specification<ProductType> spec = null;
        if (paramsNode.has("searchText")) {
            spec = getSpecification(paramsNode.get("searchText").asText());
        }
        // field
        // spec = SpecificationUtils.andParam(spec, paramsNode, "company");
        return findAll(spec, pageable).map(ProductType::toVo);
    }

    @Override
    public Specification<ProductType> getSpecification(String searchText) {
        if (!StringUtils.hasText(searchText)) {
            return null;
        }
        return (root, query, builder) -> {
            return builder.or(
                    builder.like(root.get("code"), "%" + searchText + "%"),
                    builder.like(root.get("name"), "%" + searchText + "%"));
        };
    }

    @Caching(evict = {
            @CacheEvict(key = "#p0.id"),
            @CacheEvict(key = "'all'"),
            @CacheEvict(key = "'code-'+#p0.code"),
            @CacheEvict(key = "'name-'+#p0.name"),
    })
    @Override
    public ProductType save(ProductType type) {
        return productTypeRepository.save(type);
    }

    @Caching(evict = {
            @CacheEvict(key = "#p0.id"),
            @CacheEvict(key = "'all'"),
            @CacheEvict(key = "'code-'+#p0.code"),
            @CacheEvict(key = "'name-'+#p0.name"),
    })
    @Override
    public void delete(ProductType type) {
        productTypeRepository.delete(type);
    }

    @Override
    public void updateByVo(ProductType model, ProductTypeVo vo) {
        if (model == null || vo == null) {
            throw new IllegalArgumentException("Model or VO cannot be null");
        }
        model.setName(vo.getName());
        model.setCode(vo.getCode());
    }
}
