package de.accso.flexinale.backoffice.application.services;

import de.accso.flexinale.backoffice.application.services.mapper.KinoKinoSaal2KinoKinoSaalTOMapper;
import de.accso.flexinale.backoffice.domain.dao.KinoDao;
import de.accso.flexinale.backoffice.domain.model.Kino;
import de.accso.flexinale.backoffice_api_contract.api_contract.KinoRetriever;
import de.accso.flexinale.backoffice_api_contract.api_contract.model.KinoTO;
import de.accso.flexinale.common.shared_kernel.Identifiable;
import jakarta.transaction.Transactional;

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

@Transactional
public class KinoRetrieverService implements KinoRetriever {

    private final KinoDao kinoDao;

    public KinoRetrieverService(final KinoDao kinoDao) {
        this.kinoDao = kinoDao;
    }

    @Override
    public KinoTO kino(final Identifiable.Id id) {
        Optional<Kino> kinoOptional = kinoDao.findById(id);
        return kinoOptional.map(KinoKinoSaal2KinoKinoSaalTOMapper::mapKino).orElse(null);
    }

    @Override
    public List<KinoTO> kinos() {
        List<Kino> kinos = kinoDao.findAll();
        return kinos.stream()
                .map(KinoKinoSaal2KinoKinoSaalTOMapper::mapKino)
                .collect(Collectors.toCollection(ArrayList::new));
    }
}
