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

import java.util.List;

import com.ecep.contract.QueryService;
import com.ecep.contract.constant.ServiceConstant;
import com.ecep.contract.util.SpecificationUtils;
import com.fasterxml.jackson.databind.JsonNode;
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.ds.project.model.ProjectIndustry;
import com.ecep.contract.service.VoableService;
import com.ecep.contract.vo.ProjectIndustryVo;

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

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

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

    @Cacheable(key = "'name-'+#p0")
    public ProjectIndustryVo findByName(String name) {
        ProjectIndustry industry = repository.findByName(name).orElse(null);
        if (industry != null) {
            return industry.toVo();
        }
        return null;
    }

    @Cacheable(key = "'code-'+#p0")
    public ProjectIndustryVo findByCode(String code) {
        ProjectIndustry industry = repository.findByCode(code).orElse(null);
        if (industry != null) {
            return industry.toVo();
        }
        return null;
    }

    @Cacheable(key = "'all'")
    public List<ProjectIndustryVo> findAll() {
        List<ProjectIndustry> industries = repository.findAll();
        return industries.stream().map(ProjectIndustry::toVo).toList();
    }

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

    @Override
    public Page<ProjectIndustryVo> findAll(JsonNode paramsNode, Pageable pageable) {
        Specification<ProjectIndustry> spec = null;
        if (paramsNode.has(ServiceConstant.KEY_SEARCH_TEXT)) {
            spec = getSpecification(paramsNode.get(ServiceConstant.KEY_SEARCH_TEXT).asText());
        }
        // field
        spec = SpecificationUtils.andFieldEqualParam(spec, paramsNode, "code", "name");
        Page<ProjectIndustry> page = findAll(spec, pageable);
        return page.map(ProjectIndustry::toVo);
    }

    @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 repository.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) {
        repository.delete(industry);
    }

    @Override
    public void updateByVo(ProjectIndustry model, ProjectIndustryVo vo) {
        model.setName(vo.getName());
        model.setCode(vo.getCode());
        model.setDescription(vo.getDescription());
    }
}
