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

@Lazy
@Service
@CacheConfig(cacheNames = "project-type")
public class ProjectTypeService
        implements IEntityService<ProjectType>, QueryService<ProjectTypeVo>, VoableService<ProjectType, ProjectTypeVo> {
    @Lazy
    @Autowired
    private ProjectTypeRepository repository;

    public ProjectType getById(Integer id) {
        return repository.findById(id).orElse(null);
    }

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

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

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

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

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

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

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

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

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



}
