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.ds.project.repository.ProjectIndustryRepository;
import com.ecep.contract.model.ProjectIndustry;

@Lazy
@Service
@CacheConfig(cacheNames = "project-industry")
public class ProjectIndustryService implements IEntityService<ProjectIndustry> {
    @Lazy
    @Autowired
    private ProjectIndustryRepository industryRepository;

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

    @Cacheable(key = "'name-'+#p0")
    public ProjectIndustry findByName(String name) {
        return industryRepository.findByName(name).orElse(null);
    }

    @Cacheable(key = "'code-'+#p0")
    public ProjectIndustry findByCode(String code) {
        return industryRepository.findByCode(code).orElse(null);
    }

    @Cacheable(key = "'all'")
    public List<ProjectIndustry> findAll() {
        return industryRepository.findAll();
    }

    public Page<ProjectIndustry> findAll(Specification<ProjectIndustry> spec, Pageable pageable) {
        return industryRepository.findAll(spec, pageable);
    }

    @Override
    public Specification<ProjectIndustry> 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 = "'code-'+#p0.code"),
                    @CacheEvict(key = "'name-'+#p0.name"),
                    @CacheEvict(key = "'all'"),
            }
    )
    public ProjectIndustry save(ProjectIndustry industry) {
        return industryRepository.save(industry);
    }

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