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

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

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> findByBesucher(final Identifiable.Id besucherId) {
        List<TicketEntity> ticketEntitiesForBesucher = ticketJpaRepository.findByBenutzer(besucherId.id());
        return TicketEntity2TicketMapper.map(ticketEntitiesForBesucher);
    }

    @Override
    public List<Ticket> findByVorfuehrung(final Identifiable.Id vorfuehrungId) {
        List<TicketEntity> ticketEntitiesForBesucher = ticketJpaRepository.findByVorfuehrung(vorfuehrungId.id());
        return TicketEntity2TicketMapper.map(ticketEntitiesForBesucher);
    }

    @Override
    public int gesamtZahlDerTicketsFuer(final Identifiable.Id besucherId) {
        return ticketJpaRepository.gesamtZahlDerTicketsFuer(besucherId.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();
    }
}
