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.ProductUsageRepository;
import com.ecep.contract.ds.project.model.ProductUsage;
import com.ecep.contract.service.VoableService;
import com.ecep.contract.vo.ProductUsageVo;
import com.fasterxml.jackson.databind.JsonNode;

@Lazy
@Service
@CacheConfig(cacheNames = "product-usage")
public class ProductUsageService implements IEntityService<ProductUsage>, QueryService<ProductUsageVo>,
        VoableService<ProductUsage, ProductUsageVo> {
    @Lazy
    @Autowired
    private ProductUsageRepository productUsageRepository;

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

    @Cacheable(key = "#p0")
    @Override
    public ProductUsageVo findById(Integer id) {
        ProductUsage usage = getById(id);
        if (usage != null) {
            return usage.toVo();
        }
        return null;
    }

    @Cacheable(key = "'name-'+#p0")
    public ProductUsageVo findByName(String name) {
        ProductUsage usage = productUsageRepository.findByName(name).orElse(null);
        if (usage != null) {
            return usage.toVo();
        }
        return null;
    }

    @Cacheable(key = "'code-'+#p0")
    public ProductUsageVo findByCode(String substring) {
        ProductUsage usage = productUsageRepository.findByCode(substring).orElse(null);
        if (usage != null) {
            return usage.toVo();
        }
        return null;
    }

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

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

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

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

    @Caching(evict = {
            @CacheEvict(key = "#p0.id"),
            @CacheEvict(key = "'all'"),
            @CacheEvict(key = "'name-'+#p0.name"),
            @CacheEvict(key = "'code-'+#p0.code")
    })
    public ProductUsage save(ProductUsage usage) {
        return productUsageRepository.save(usage);
    }

    @Caching(evict = {
            @CacheEvict(key = "#p0.id"),
            @CacheEvict(key = "'all'"),
            @CacheEvict(key = "'name-'+#p0.name"),
            @CacheEvict(key = "'code-'+#p0.code")
    })
    public void delete(ProductUsage usage) {
        productUsageRepository.delete(usage);
    }

    @Override
    public void updateByVo(ProductUsage entity, ProductUsageVo vo) {
        entity.setName(vo.getName());
        entity.setCode(vo.getCode());
        entity.setDescription(vo.getDescription());

    }

}
