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

import io.gmss.fiscad.entities.infocad.parametre.TypeRepresentation;
import io.gmss.fiscad.exceptions.BadRequestException;
import io.gmss.fiscad.exceptions.NotFoundException;
import io.gmss.fiscad.interfaces.infocad.parametre.TypeRepresentationService;
import io.gmss.fiscad.repositories.infocad.parametre.TypeRepresentationRepository;
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 TypeRepresentationServiceImpl implements TypeRepresentationService {

    private final TypeRepresentationRepository typeRepresentationRepository;

    public TypeRepresentationServiceImpl(TypeRepresentationRepository typeRepresentationRepository) {
        this.typeRepresentationRepository = typeRepresentationRepository;
    }

    @Override
    public TypeRepresentation createTypeRepresentation(TypeRepresentation typeRepresentation) throws BadRequestException {
        if (typeRepresentation.getId() != null) {
            throw new BadRequestException("Impossible de créer un nouveau type de représentation ayant un id non null.");
        }
        return typeRepresentationRepository.save(typeRepresentation);
    }

    @Override
    public TypeRepresentation updateTypeRepresentation(Long id, TypeRepresentation typeRepresentation) throws NotFoundException {
        if (typeRepresentation.getId() == null) {
            throw new BadRequestException("Impossible de mettre à jour un nouveau type de représentation ayant un id null.");
        }
        if (!typeRepresentationRepository.existsById(typeRepresentation.getId())) {
            throw new NotFoundException("Impossible de trouver le type de représentation spécifié dans notre base de données.");
        }
        return typeRepresentationRepository.save(typeRepresentation);
    }

    @Override
    public void deleteTypeRepresentation(Long id) throws NotFoundException {
        Optional<TypeRepresentation> typeRepresentationOptional = typeRepresentationRepository.findById(id);
        if (typeRepresentationOptional.isPresent()) {
            typeRepresentationRepository.deleteById(typeRepresentationOptional.get().getId());
        } else {
            throw new NotFoundException("Impossible de trouver le type de représentation spécifié dans notre base de données.");
        }
    }

    @Override
    public Page<TypeRepresentation> getTypeRepresentationList(Pageable pageable) {
        return typeRepresentationRepository.findAll(pageable);
    }

    @Override
    public List<TypeRepresentation> getTypeRepresentationList() {
        return typeRepresentationRepository.findAll();
    }

    @Override
    public Optional<TypeRepresentation> getTypeRepresentationById(Long id) {
        if (typeRepresentationRepository.existsById(id)) {
            return typeRepresentationRepository.findById(id);
        } else {
            throw new NotFoundException("Impossible de trouver le type de représentation spécifié dans la base de données.");
        }

    }
}
