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

import com.ecep.contract.CompanyFileType;
import com.ecep.contract.VendorType;
import com.ecep.contract.constant.ServiceConstant;
import com.ecep.contract.util.SpecificationUtils;
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 com.ecep.contract.IEntityService;
import com.ecep.contract.QueryService;
import com.ecep.contract.ds.company.repository.CompanyFileTypeLocalRepository;
import com.ecep.contract.model.CompanyFileTypeLocal;
import com.fasterxml.jackson.databind.JsonNode;

import jakarta.annotation.Resource;
import org.springframework.util.StringUtils;

import java.util.Locale;
import java.util.Map;

@Lazy
@Service
@CacheConfig(cacheNames = "company-file-type")
public class CompanyFileTypeService
        implements IEntityService<CompanyFileTypeLocal>, QueryService<CompanyFileTypeLocal> {

    @Resource
    private CompanyFileTypeLocalRepository repository;

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

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

        if (paramsNode.has("type")) {
            spec = SpecificationUtils.and(spec, (root, query, builder) -> builder.equal(root.get("type"), CompanyFileType.valueOf(paramsNode.get("type").asText())));
        }
        // field
        return findAll(spec, pageable);
    }

    @Cacheable(key = "'all-'+#p0.toLanguageTag()")
    public Map<CompanyFileType, CompanyFileTypeLocal> findAll(Locale locale) {
        return repository.getCompleteMapByLocal(locale.toLanguageTag());
    }


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

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

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

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