package com.ecep.contract.ds.contract.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.domain.Sort;
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.contract.repository.ContractBidVendorRepository;
import com.ecep.contract.model.Company;
import com.ecep.contract.model.Contract;
import com.ecep.contract.model.ContractBidVendor;
import com.ecep.contract.util.SpecificationUtils;
import com.fasterxml.jackson.databind.JsonNode;

@Lazy
@Service
@CacheConfig(cacheNames = "contract-ven-bid")
public class ContractBidVendorService implements IEntityService<ContractBidVendor>, QueryService<ContractBidVendor> {
    @Lazy
    @Autowired
    private ContractBidVendorRepository repository;

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

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

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

    @Cacheable(key = "'allbycontract-'+#p0.id")
    public List<ContractBidVendor> findByContract(Contract contract) {
        return repository.findByContractId(contract.getId());
    }

    @Caching(evict = {
            @CacheEvict(key = "#p0.id"),
            @CacheEvict(key = "'allbycontract-'+#p0.contract.id")
    })
    public ContractBidVendor save(ContractBidVendor bidVendor) {
        return repository.save(bidVendor);
    }

    @Caching(evict = {
            @CacheEvict(key = "#p0.id"),
            @CacheEvict(key = "'allbycontract-'+#p0.contract.id")
    })
    @Override
    public void delete(ContractBidVendor bidVendor) {
        repository.delete(bidVendor);
    }

    public List<ContractBidVendor> findAll(Specification<ContractBidVendor> spec, Sort sort) {
        return repository.findAll(spec, sort);
    }

    @Caching(evict = {
            @CacheEvict(key = "#p0.id")
    })
    public void deleteById(int id) {
        repository.deleteById(id);
    }

    public List<ContractBidVendor> findByContractAndCompany(Contract contract, Company company) {
        return repository.findByContractIdAndCompanyId(contract.getId(), company.getId());
    }

    @Override
    public Page<ContractBidVendor> findAll(JsonNode paramsNode, Pageable pageable) {
        Specification<ContractBidVendor> spec = null;
        if (paramsNode.has("searchText")) {
            spec = getSpecification(paramsNode.get("searchText").asText());
        }

        // field
        spec = SpecificationUtils.andParam(spec, paramsNode, "contract");
        return findAll(spec, pageable);
    }
}
