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

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

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.CompanyVendorApprovedItemRepository;
import com.ecep.contract.model.Company;
import com.ecep.contract.model.CompanyOldName;
import com.ecep.contract.model.CompanyVendor;
import com.ecep.contract.model.CompanyVendorApprovedItem;
import com.ecep.contract.model.CompanyVendorApprovedList;

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

@Lazy
@Service
public class CompanyVendorApprovedItemService implements IEntityService<CompanyVendorApprovedItem> {
    @Lazy
    @Autowired
    private CompanyVendorApprovedItemRepository repository;
    @Lazy
    @Autowired
    private CompanyOldNameService companyOldNameService;

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

    @Override
    public Specification<CompanyVendorApprovedItem> getSpecification(String searchText) {
        return (root, query, builder) -> {
            Path<CompanyVendor> 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<CompanyVendorApprovedItem> findAll(Specification<CompanyVendorApprovedItem> spec, Pageable pageable) {
        return repository.findAll(spec, pageable);
    }

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

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

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

    public List<CompanyVendorApprovedItem> findAllByListAndVendor(CompanyVendorApprovedList approvedList, CompanyVendor vendor) {
        return repository.findAllByListAndVendor(approvedList, vendor);
    }
}
