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

import de.accso.flexinale.common.shared_kernel.Identifiable;
import de.accso.flexinale.ticketing.domain.model.Ticket;
import de.accso.flexinale.ticketing.infrastructure.persistence.TicketEntity;

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

public final class TicketEntity2TicketMapper {

    public static Ticket map(final TicketEntity ticketEntity) {
        return new Ticket(ticketEntity.id(), ticketEntity.version(),
                Identifiable.Id.of(ticketEntity.filmId),
                Identifiable.Id.of(ticketEntity.vorfuehrungId),
                Identifiable.Id.of(ticketEntity.benutzerId),
                ticketEntity.verkaufsKanal);
    }

    @SuppressWarnings("OptionalUsedAsFieldOrParameterType")
    public static Optional<Ticket> map(final Optional<TicketEntity> optionalTicketEntity) {
        if (optionalTicketEntity.isEmpty()) {
            return Optional.empty();
        }
        else {
            TicketEntity ticketEntity = optionalTicketEntity.get();
            return Optional.of(map(ticketEntity));
        }
    }

    public static TicketEntity map(final Ticket ticket) {
        return new TicketEntity(ticket.id().id(), ticket.version().version(),
                ticket.filmId.id(), ticket.vorfuehrungId.id(), ticket.besucherId.id(),
                ticket.verkaufsKanal);
    }

    public static List<Ticket> map(final List<TicketEntity> ticketEntities) {
        return ticketEntities.stream().map(TicketEntity2TicketMapper::map)
                .collect(Collectors.toCollection(ArrayList::new));
    }
}
