package de.accso.flexinale.backoffice.infrastructure.persistence;

import de.accso.flexinale.backoffice.domain.dao.KinoDao;
import de.accso.flexinale.backoffice.domain.model.Kino;
import de.accso.flexinale.backoffice.infrastructure.persistence.mapper.KinoKinoSaalEntity2KinoKinoSaalMapper;
import de.accso.flexinale.common.shared_kernel.Identifiable;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@SuppressWarnings("unused")
public class KinoJpaRepositoryDelegate implements KinoDao {

    private final KinoJpaRepository kinoJpaRepository;

    public KinoJpaRepositoryDelegate(final KinoJpaRepository KinoJpaRepository) {
        this.kinoJpaRepository = KinoJpaRepository;
    }

    @Override
    public List<Kino> findAll() {
        List<KinoEntity> kinoEntities = kinoJpaRepository.findAll();

        return kinoEntities.stream()
                .map(KinoKinoSaalEntity2KinoKinoSaalMapper::mapKino)
                .collect(Collectors.toCollection(ArrayList::new));
    }

    @Override
    public Optional<Kino> findById(final Identifiable.Id id) {
        Optional<KinoEntity> kinoEntity = kinoJpaRepository.findById(id.id());
        return KinoKinoSaalEntity2KinoKinoSaalMapper.mapKino(kinoEntity);
    }

    @Override
    public Kino save(final Kino kino) {
        KinoEntity kinoEntity = KinoKinoSaalEntity2KinoKinoSaalMapper.mapKino(kino);
        KinoEntity savedEntity = kinoJpaRepository.save(kinoEntity);
        return KinoKinoSaalEntity2KinoKinoSaalMapper.mapKino(savedEntity);
    }

    @Override
    public void delete(final Kino kino) {
        KinoEntity kinoEntity = KinoKinoSaalEntity2KinoKinoSaalMapper.mapKino(kino);
        kinoJpaRepository.delete(kinoEntity);
    }

    @Override
    public void deleteById(final Identifiable.Id id) {
        kinoJpaRepository.deleteById(id.id());
    }

    @Override
    public void deleteAll() {
        kinoJpaRepository.deleteAll();
    }
}
