package de.accso.flexinale.infrastructure.persistence;

import de.accso.flexinale.domain.dao.TicketDao;
import de.accso.flexinale.domain.model.Besucher;
import de.accso.flexinale.domain.model.Ticket;
import de.accso.flexinale.infrastructure.persistence.mapper.TicketEntity2TicketMapper;
import de.accso.flexinale.shared_kernel.Identifiable;

import java.util.List;
import java.util.Optional;

@SuppressWarnings("unused")
public class TicketJpaRepositoryDelegate implements TicketDao {

    private final TicketJpaRepository ticketJpaRepository;

    public TicketJpaRepositoryDelegate(TicketJpaRepository ticketJpaRepository) {
        this.ticketJpaRepository = ticketJpaRepository;
    }

    @Override
    public List<Ticket> findAll() {
        List<TicketEntity> ticketEntities = ticketJpaRepository.findAll();
        return TicketEntity2TicketMapper.map(ticketEntities);
    }

    @Override
    public Optional<Ticket> findById(final Identifiable.Id id) {
        Optional<TicketEntity> ticketEntity = ticketJpaRepository.findById(id.id());
        return TicketEntity2TicketMapper.map(ticketEntity);
    }

    @Override
    public List<Ticket> findByBesucherOrderByZeit(final Besucher besucher) {
        List<TicketEntity> ticketEntitiesForBesucher = ticketJpaRepository.findByBenutzerOrderByZeit(besucher.id().id());
        return TicketEntity2TicketMapper.map(ticketEntitiesForBesucher);
    }

    @Override
    public int gesamtZahlDerTicketsFuer(final Besucher besucher) {
        return ticketJpaRepository.gesamtZahlDerTicketsFuer(besucher.id().id());
    }

    @Override
    public Ticket save(final Ticket ticket) {
        TicketEntity ticketEntity = TicketEntity2TicketMapper.map(ticket);
        TicketEntity savedEntity = ticketJpaRepository.save(ticketEntity);
        return TicketEntity2TicketMapper.map(savedEntity);
    }

    @Override
    public void delete(final Ticket ticket) {
        TicketEntity ticketEntity = TicketEntity2TicketMapper.map(ticket);
        ticketJpaRepository.delete(ticketEntity);
    }

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

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