package com.ecep.contract.ds.customer.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.ds.customer.repository.CompanyCustomerEntityRepository;
import com.ecep.contract.model.CompanyCustomer;
import com.ecep.contract.model.CompanyCustomerEntity;
import com.ecep.contract.util.SpecificationUtils;

@Lazy
@Service
@CacheConfig(cacheNames = "company-customer-entity")
public class CompanyCustomerEntityService implements IEntityService<CompanyCustomerEntity> {
    @Lazy
    @Autowired
    private CompanyCustomerEntityRepository repository;

    @Cacheable(key = "#p0")
    @Override
    public CompanyCustomerEntity findById(Integer id) {
        return repository.findById(id).orElse(null);
    }

    @Override
    public Specification<CompanyCustomerEntity> getSpecification(String searchText) {
        if (!StringUtils.hasText(searchText)) {
            return null;
        }
        return SpecificationUtils.andWith(searchText, this::buildSearchSpecification);
    }

    protected Specification<CompanyCustomerEntity> buildSearchSpecification(String searchText) {
        return (root, query, builder) -> {
            return builder.or(
                    builder.like(root.get("name"), "%" + searchText + "%"),
                    builder.like(root.get("abbName"), "%" + searchText + "%"),
                    builder.like(root.get("code"), "%" + searchText + "%"));
        };
    }

    public List<CompanyCustomerEntity> search(String searchText) {
        Specification<CompanyCustomerEntity> spec = getSpecification(searchText);
        return repository.findAll(spec, Pageable.ofSize(10)).getContent();
    }

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

    @Caching(evict = {
            @CacheEvict(key = "#p0.id"),
            @CacheEvict(key = "'code-'+#p0.code")
    })
    @Override
    public void delete(CompanyCustomerEntity entity) {
        repository.delete(entity);
    }

    @Caching(evict = {
            @CacheEvict(key = "#p0.id"),
            @CacheEvict(key = "'code-'+#p0.code")
    })
    @Override
    public CompanyCustomerEntity save(CompanyCustomerEntity entity) {
        return repository.save(entity);
    }

    public List<CompanyCustomerEntity> findAllByCustomer(CompanyCustomer companyCustomer) {
        return repository.findByCustomer(companyCustomer);
    }

    @Cacheable(key = "'code-'+#p0")
    public CompanyCustomerEntity findByCustomerCode(String customerCode) {
        return repository.findByCode(customerCode).orElse(null);
    }

    public void resetTo(CompanyCustomer from, CompanyCustomer to) {
        List<CompanyCustomerEntity> entities = repository.findByCustomer(from);
        for (CompanyCustomerEntity entity : entities) {
            entity.setCustomer(to);
        }
        repository.saveAll(entities);
    }

}
