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

import java.util.List;
import java.util.stream.Collectors;

import com.ecep.contract.QueryService;
import com.ecep.contract.util.SpecificationUtils;
import com.fasterxml.jackson.databind.JsonNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import com.ecep.contract.IEntityService;
import com.ecep.contract.ds.company.service.CompanyOldNameService;
import com.ecep.contract.ds.vendor.repository.VendorApprovedItemRepository;
import com.ecep.contract.model.Company;
import com.ecep.contract.model.CompanyOldName;
import com.ecep.contract.model.Vendor;
import com.ecep.contract.model.VendorApprovedItem;
import com.ecep.contract.model.VendorApproved;

import jakarta.persistence.criteria.Path;
import jakarta.persistence.criteria.Predicate;

@Lazy
@Service
public class VendorApprovedItemService implements IEntityService<VendorApprovedItem>, QueryService<VendorApprovedItem> {
    @Lazy
    @Autowired
    private VendorApprovedItemRepository repository;
    @Lazy
    @Autowired
    private CompanyOldNameService companyOldNameService;

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

    @Override
    public Specification<VendorApprovedItem> getSpecification(String searchText) {
        return (root, query, builder) -> {
            Path<Vendor> vendor = root.get("vendor");
            Path<Company> company = vendor.get("company");

            List<CompanyOldName> oldNames = companyOldNameService.search(searchText);
            Predicate companyPredicate = oldNames.isEmpty() ? builder.like(company.get("name"), "%" + searchText + "%") : builder.or(
                    builder.like(company.get("name"), "%" + searchText + "%"),
                    company.get("id").in(oldNames.stream().map(CompanyOldName::getCompanyId).collect(Collectors.toList()))
            );
            return builder.or(
                    builder.and(
                            vendor.isNotNull(),
                            builder.and(
                                    company.isNotNull(),
                                    companyPredicate
                            )),
                    builder.like(root.get("vendorName"), "%" + searchText + "%"),
                    builder.like(root.get("description"), "%" + searchText + "%")
            );
        };
    }

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

    @Override
    public void delete(VendorApprovedItem entity) {
        repository.delete(entity);
    }

    @Override
    public VendorApprovedItem save(VendorApprovedItem entity) {
        return repository.save(entity);
    }

    public List<VendorApprovedItem> findAll(Specification<VendorApprovedItem> spec, Sort sort) {
        return repository.findAll(spec, sort);
    }

    @Override
    public Page<VendorApprovedItem> findAll(JsonNode paramsNode, Pageable pageable) {
        Specification<VendorApprovedItem> spec = null;
        if (paramsNode.has("searchText")) {
            spec = getSpecification(paramsNode.get("searchText").asText());
        }
        // 添加额外的参数过滤
        spec = SpecificationUtils.andParam(spec, paramsNode, "vendor", "list");
        return findAll(spec, pageable);
    }

    public List<VendorApprovedItem> findAllByListAndVendor(VendorApproved approvedList, Vendor vendor) {
        return repository.findAllByListAndVendor(approvedList, vendor);
    }
}
