package io.gmss.fiscad.implementations.rfu.parametre;

import io.gmss.fiscad.entities.rfu.parametre.BaremRfuNonBati;
import io.gmss.fiscad.exceptions.BadRequestException;
import io.gmss.fiscad.exceptions.NotFoundException;
import io.gmss.fiscad.interfaces.rfu.parametre.BaremRfuNonBatiService;
import io.gmss.fiscad.repositories.rfu.parametre.BaremRfuNonBatiRepository;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
public class BaremRfuNonBatiServiceImpl implements BaremRfuNonBatiService {
    private final BaremRfuNonBatiRepository baremRfuNonBatiRepository;

    public BaremRfuNonBatiServiceImpl(BaremRfuNonBatiRepository baremRfuNonBatiRepository) {
        this.baremRfuNonBatiRepository = baremRfuNonBatiRepository;
    }

    @Override
    public BaremRfuNonBati createBaremRfuNonBati(BaremRfuNonBati baremRfuNonBati) throws BadRequestException {
        if (baremRfuNonBati.getId() != null) {
            throw new BadRequestException("Impossible de créer une nouvelle campgne ayant un id non null.");
        }
        return baremRfuNonBatiRepository.save(baremRfuNonBati);
    }
    @Override
    public BaremRfuNonBati updateBaremRfuNonBati(Long id, BaremRfuNonBati baremRfuNonBati) throws NotFoundException {
        if (baremRfuNonBati.getId() == null) {
            throw new BadRequestException("Impossible de mettre à jour une nouvelle baremRfuNonBati ayant un id null.");
        }
        if (!baremRfuNonBatiRepository.existsById(baremRfuNonBati.getId())) {
            throw new NotFoundException("Impossible de trouver la baremRfuNonBati spécifiée dans notre base de données.");
        }
        return baremRfuNonBatiRepository.save(baremRfuNonBati);
    }

    @Override
    public void deleteBaremRfuNonBati(Long id) throws NotFoundException {
        Optional<BaremRfuNonBati> baremRfuNonBatiOptional = baremRfuNonBatiRepository.findById(id);
        if (baremRfuNonBatiOptional.isPresent()) {
            baremRfuNonBatiRepository.deleteById(baremRfuNonBatiOptional.get().getId());
        } else {
            throw new NotFoundException("Impossible de trouver la baremRfuNonBati spécifiée dans notre base de données.");
        }
    }
    @Override
    public Page<BaremRfuNonBati> getBaremRfuNonBatiList(Pageable pageable) {
        return baremRfuNonBatiRepository.findAll(pageable);
    }

    @Override
    public List<BaremRfuNonBati> getBaremRfuNonBatiList() {
        return baremRfuNonBatiRepository.findAll();
    }



    @Override
    public Optional<BaremRfuNonBati> getBaremRfuNonBatiById(Long id) {
        return baremRfuNonBatiRepository.findById(id);
    }
}

