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

import de.accso.flexinale.backoffice.domain.dao.FilmDao;
import de.accso.flexinale.backoffice.domain.model.Film;
import de.accso.flexinale.backoffice.infrastructure.persistence.mapper.FilmEntity2FilmMapper;
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 FilmJpaRepositoryDelegate implements FilmDao {

    private final FilmJpaRepository filmJpaRepository;

    public FilmJpaRepositoryDelegate(FilmJpaRepository filmJpaRepository) {
        this.filmJpaRepository = filmJpaRepository;
    }

    @Override
    public List<Film> findAll() {
        List<FilmEntity> filmEntities = filmJpaRepository.findAll();

        return filmEntities.stream()
                .map(FilmEntity2FilmMapper::map)
                .collect(Collectors.toCollection(ArrayList::new));
    }

    @Override
    public Optional<Film> findById(final Identifiable.Id id) {
        Optional<FilmEntity> filmEntity = filmJpaRepository.findById(id.id());
        return FilmEntity2FilmMapper.map(filmEntity);
    }

    @Override
    public Film save(final Film film) {
        FilmEntity filmEntity = FilmEntity2FilmMapper.map(film);
        FilmEntity savedEntity = filmJpaRepository.save(filmEntity);
        return FilmEntity2FilmMapper.map(savedEntity);
    }

    @Override
    public void delete(final Film film) {
        FilmEntity filmEntity = FilmEntity2FilmMapper.map(film);
        filmJpaRepository.delete(filmEntity);
    }

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

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