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

import de.accso.flexinale.common.shared_kernel.Identifiable;
import de.accso.flexinale.ticketing.domain.dao.KontingentDao;
import de.accso.flexinale.ticketing.domain.model.Kontingent;
import de.accso.flexinale.ticketing.infrastructure.persistence.mapper.KontingentEntity2KontingentMapper;

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

@SuppressWarnings("unused")
public class KontingentJpaRepositoryDelegate implements KontingentDao {

    private final KontingentJpaRepository kontingentJpaRepository;

    public KontingentJpaRepositoryDelegate(KontingentJpaRepository kontingentJpaRepository) {
        this.kontingentJpaRepository = kontingentJpaRepository;
    }

    @Override
    public List<Kontingent> findAll() {
        List<KontingentEntity> KontingentEntities = kontingentJpaRepository.findAll();

        return KontingentEntities.stream()
                .map(KontingentEntity2KontingentMapper::map)
                .collect(Collectors.toCollection(ArrayList::new));
    }

    @Override
    public Optional<Kontingent> findById(final Identifiable.Id id) {
        Optional<KontingentEntity> kontingentEntity = kontingentJpaRepository.findById(id.id());
        return KontingentEntity2KontingentMapper.map(kontingentEntity);
    }

    @Override
    public Optional<Kontingent> findByVorfuehrung(final Identifiable.Id vorfuehrungId) {
        Optional<KontingentEntity> kontingentEntity = kontingentJpaRepository.findByVorfuehrung(vorfuehrungId.id());
        return KontingentEntity2KontingentMapper.map(kontingentEntity);
    }

    @Override
    public Kontingent save(final Kontingent kontingent) {
        KontingentEntity kontingentEntity = KontingentEntity2KontingentMapper.map(kontingent);
        KontingentEntity savedEntity = kontingentJpaRepository.save(kontingentEntity);
        return KontingentEntity2KontingentMapper.map(savedEntity);
    }

    @Override
    public void delete(final Kontingent kontingent) {
        KontingentEntity kontingentEntity = KontingentEntity2KontingentMapper.map(kontingent);
        kontingentJpaRepository.delete(kontingentEntity);
    }

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

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