package org.egl_cepgl.pm.service;

import lombok.extern.slf4j.Slf4j;
import org.egl_cepgl.pm.dto.FileCategoryDto;
import org.egl_cepgl.pm.model.Enterprise;
import org.egl_cepgl.pm.model.FileCategory;
import org.egl_cepgl.pm.repository.FileCategoryRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@Slf4j
@Service
public class FileCategoryService {

    private FileCategoryRepository repository;

    @Autowired
    public FileCategoryService(FileCategoryRepository repository) {
        this.repository = repository;
    }

    public FileCategory add(FileCategory category)
    {
        log.info("ENREGGGGG=======ENG++"+category.toString());
        return this.repository.save(category);
    }

    public Page<FileCategory> findAll(String search, int page, int size)
    {
        Pageable paging = PageRequest.of(page, size);
        Page<FileCategory> objs;
        if (search == null) {
            objs = this.repository.findAll(paging);
        } else {
            objs = this.repository.findByNamepContaining(search, paging);
        }
        return objs;
    }

    public FileCategory update(FileCategory category)
    {
        return this.repository.save(category);
    }

    public void delete(Long id)
    {
        if(id == null){
            log.error("ID est null");
            return;
        }
        repository.deleteById(id);
    }
}
