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

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
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 org.springframework.util.StringUtils;

import com.ecep.contract.IEntityService;
import com.ecep.contract.ds.other.repository.EmployeeAuthBindRepository;
import com.ecep.contract.model.Employee;
import com.ecep.contract.model.EmployeeAuthBind;

@Lazy
@Service
@CacheConfig(cacheNames = "employee-auth-bind")
public class EmployeeAuthBindService implements IEntityService<EmployeeAuthBind> {
    @Lazy
    @Autowired
    private EmployeeAuthBindRepository repository;

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

    @Override
    public Specification<EmployeeAuthBind> getSpecification(String searchText) {
        if (!StringUtils.hasText(searchText)) {
            return null;
        }
        return (root, query, builder) -> {
            return builder.or(
                    builder.like(root.get("ip"), "%" + searchText + "%"),
                    builder.like(root.get("mac"), "%" + searchText + "%"),
                    builder.like(root.get("description"), "%" + searchText + "%")
            );
        };
    }

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

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

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

    public List<EmployeeAuthBind> findAllByEmployee(Employee employee, Sort sort) {
        return repository.findAllByEmployee(employee, sort);
    }
}
