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

import java.util.List;

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.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import com.ecep.contract.IEntityService;
import com.ecep.contract.ds.vendor.repository.CompanyVendorApprovedFileRepository;
import com.ecep.contract.model.CompanyVendorApprovedFile;
import com.ecep.contract.model.CompanyVendorApprovedList;

@Lazy
@Service
public class CompanyVendorApprovedFileService implements IEntityService<CompanyVendorApprovedFile> {
    @Lazy
    @Autowired
    private CompanyVendorApprovedFileRepository repository;

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

    @Override
    public Specification<CompanyVendorApprovedFile> getSpecification(String searchText) {
        return (root, query, builder) -> {
            return builder.or(
                    builder.like(root.get("fileName"), "%" + searchText + "%"),
                    builder.like(root.get("description"), "%" + searchText + "%")
            );
        };
    }

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

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

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

    public CompanyVendorApprovedFile findByName(CompanyVendorApprovedList approvedList, String name) {
        return repository.findByListIdAndFileName(approvedList.getId(), name);
    }

    public List<CompanyVendorApprovedFile> findAllByList(CompanyVendorApprovedList list) {
        return repository.findAllByListId(list.getId());
    }
}
