package org.egl_cepgl.pm.service;

import lombok.extern.slf4j.Slf4j;
import org.egl_cepgl.pm.dto.ApplicantDto;
import org.egl_cepgl.pm.dto.CountryDto;
import org.egl_cepgl.pm.model.Applicant;
import org.egl_cepgl.pm.model.ApplicantQualification;
import org.egl_cepgl.pm.model.Country;
import org.egl_cepgl.pm.repository.ApplicantRepository;
import org.egl_cepgl.pm.repository.CountryRepository;
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.stream.Collectors;

@Slf4j
@Service
public class CountryService {

    private CountryRepository repository;

    @Autowired
    public CountryService(CountryRepository repository) {
        this.repository = repository;
    }

    public Country add(Country obj)
    {
        log.info("ENREGGGGG=======ENG++"+obj.toString());

        return repository.save(obj);
    }

    public Page<Country> findAll(String search, int page, int size)
    {
        Pageable paging = PageRequest.of(page, size);
        Page<Country> objs;
        if (search == null) {
            objs = this.repository.findAll(paging);
        } else {
            objs = this.repository.findAllByNamepContaining(search, paging);
        }
        return objs;
    }

    public Country update(Country obj)
    {
        return this.repository.save(obj);
    }

    public void delete(Long id)
    {
        if(id == null){
            log.error("ID est null");
            return;
        }
        repository.deleteById(id);
    }
}
