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

import io.gmss.fiscad.entities.infocad.parametre.ModeAcquisition;
import io.gmss.fiscad.exceptions.BadRequestException;
import io.gmss.fiscad.exceptions.NotFoundException;
import io.gmss.fiscad.interfaces.infocad.parametre.ModeAcquisitionService;
import io.gmss.fiscad.repositories.infocad.parametre.ModeAcquisitionRepository;
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 ModeAcquisitionServiceImpl implements ModeAcquisitionService {

    private final ModeAcquisitionRepository modeAcquisitionRepository;

    public ModeAcquisitionServiceImpl(ModeAcquisitionRepository modeAcquisitionRepository) {
        this.modeAcquisitionRepository = modeAcquisitionRepository;
    }

    @Override
    public ModeAcquisition createModeAcquisition(ModeAcquisition modeAcquisition) throws BadRequestException {
        if (modeAcquisition.getId() != null) {
            throw new BadRequestException("Impossible de créer un nouveau mode d'acquisition ayant un id non null.");
        }
        return modeAcquisitionRepository.save(modeAcquisition);
    }

    @Override
    public ModeAcquisition updateModeAcquisition(Long id, ModeAcquisition modeAcquisition) throws NotFoundException {
        if (modeAcquisition.getId() == null) {
            throw new BadRequestException("Impossible de mettre à jour un nouveau mode d'acquisition ayant un id null.");
        }
        if (!modeAcquisitionRepository.existsById(modeAcquisition.getId())) {
            throw new NotFoundException("Impossible de trouver le mode d'acquisition spécifié dans notre base de données.");
        }
        return modeAcquisitionRepository.save(modeAcquisition);
    }

    @Override
    public void deleteModeAcquisition(Long id) throws NotFoundException {
        Optional<ModeAcquisition> modeAcquisitionOptional = modeAcquisitionRepository.findById(id);
        if (modeAcquisitionOptional.isPresent()) {
            modeAcquisitionRepository.deleteById(modeAcquisitionOptional.get().getId());
        } else {
            throw new NotFoundException("Impossible de trouver le mode d'acquisition spécifié dans notre base de données.");
        }
    }

    @Override
    public Page<ModeAcquisition> getModeAcquisitionList(Pageable pageable) {
        return modeAcquisitionRepository.findAll(pageable);
    }

    @Override
    public List<ModeAcquisition> getModeAcquisitionList() {
        return modeAcquisitionRepository.findAll();
    }

    @Override
    public Optional<ModeAcquisition> getModeAcquisitionById(Long id) {
        if (modeAcquisitionRepository.existsById(id)) {
            return modeAcquisitionRepository.findById(id);
        } else {
            throw new NotFoundException("Impossible de trouver le mode d'acquisition spécifié dans la base de données.");
        }

    }
}
