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.SpringApp;
import com.ecep.contract.constant.ServiceConstant;
import com.ecep.contract.ds.other.service.InventoryService;
import com.ecep.contract.ds.project.repository.ProjectCostItemRepository;
import com.ecep.contract.ds.project.model.ProjectCost;
import com.ecep.contract.ds.project.model.ProjectCostItem;
import com.ecep.contract.service.VoableService;
import com.ecep.contract.util.SpecificationUtils;
import com.ecep.contract.vo.ProjectCostItemVo;
import com.fasterxml.jackson.databind.JsonNode;

@Lazy
@Service
@CacheConfig(cacheNames = "project-cost-item")
public class ProjectCostItemService implements IEntityService<ProjectCostItem>, QueryService<ProjectCostItemVo>,
        VoableService<ProjectCostItem, ProjectCostItemVo> {
    @Lazy
    @Autowired
    private ProjectCostItemRepository repository;

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

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

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

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

        // field
        spec = SpecificationUtils.andParam(spec, paramsNode, "cost");

        Page<ProjectCostItem> page = findAll(spec, pageable);
        return page.map(ProjectCostItem::toVo);
    }

    @Override
    public Specification<ProjectCostItem> 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"),
    })
    public ProjectCostItem save(ProjectCostItem item) {
        return repository.save(item);
    }

    @Caching(evict = {
            @CacheEvict(key = "#p0.id"),
    })
    public void delete(ProjectCostItem item) {
        repository.delete(item);
    }

    public List<ProjectCostItem> findByCostId(Integer costId) {
        if (costId == null) {
            return List.of();
        }
        ProjectCost cost = new ProjectCost();
        cost.setId(costId);
        return repository.findByCost(cost);
    }

    @Override
    public void updateByVo(ProjectCostItem model, ProjectCostItemVo vo) {
        model.setTitle(vo.getTitle());
        model.setSpecification(vo.getSpecification());
        model.setUnit(vo.getUnit());
        model.setInTaxRate(vo.getInTaxRate());
        model.setInExclusiveTaxPrice(vo.getInExclusiveTaxPrice());
        model.setInQuantity(vo.getInQuantity());
        model.setOutTaxRate(vo.getOutTaxRate());
        model.setOutExclusiveTaxPrice(vo.getOutExclusiveTaxPrice());
        model.setOutQuantity(vo.getOutQuantity());
        model.setRemark(vo.getRemark());

        if (vo.getCostId() != null) {
            ProjectCost cost = SpringApp.getBean(ProjectCostService.class).getById(vo.getCostId());
            model.setCost(cost);
        }

        if (vo.getInventoryId() != null) {
            InventoryService inventoryService = SpringApp.getBean(InventoryService.class);
            model.setInventory(inventoryService.getById(vo.getInventoryId()));
        } else {
            model.setInventory(null);
        }
    }
}
