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

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.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.EmployeeLoginHistoryRepository;
import com.ecep.contract.model.EmployeeLoginHistory;

@Lazy
@Service
@CacheConfig(cacheNames = "employee-login-history")
public class EmployeeLoginHistoryService implements IEntityService<EmployeeLoginHistory> {
    @Lazy
    @Autowired
    private EmployeeLoginHistoryRepository repository;

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

    @Override
    public Specification<EmployeeLoginHistory> 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 + "%")
            );
        };
    }

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

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

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