package com.ecep.contract.service;

import java.util.List;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import com.ecep.contract.model.Company;
import com.ecep.contract.util.ParamUtils;
import com.ecep.contract.util.ParamUtils.Builder;
import com.ecep.contract.vm.CompanyContactViewModel;
import com.ecep.contract.vo.CompanyContactVo;
import com.ecep.contract.vo.CompanyVo;

@Service
public class CompanyContactService extends QueryService<CompanyContactVo, CompanyContactViewModel> {

    public List<CompanyContactVo> searchByCompany(Company company, String searchText) {
        Builder params = getSpecification(searchText);
        params.equals("company", company);
        List<CompanyContactVo> list = findAll(params.build(), Pageable.ofSize(10)).getContent();
        return list;
    }

    public CompanyContactVo findFirstByCompany(CompanyVo company) {
        Builder params = ParamUtils.builder();
        params.equals("company", company);
        Page<CompanyContactVo> page = findAll(params.build(), Pageable.unpaged());
        if (page.isEmpty()) {
            return null;
        }
        return page.getContent().getFirst();
    }

}
