package de.accso.flexinale.data.model;

import de.accso.flexinale.shared_kernel.FlexinaleIllegalArgumentException;
import de.accso.flexinale.shared_kernel.Identifiable;
import de.accso.flexinale.util.Config;
import jakarta.persistence.CascadeType;
import jakarta.persistence.FetchType;
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;

import java.io.Serializable;
import java.time.LocalDateTime;

@jakarta.persistence.Entity
public final class Vorfuehrung implements Identifiable, Serializable {  // TODO is only final as otherwise Spotbugs complains, get rid of Exception in constructor!
    @jakarta.persistence.Id
    public String id;  // primary key

    @jakarta.persistence.Column(name = "zeit", columnDefinition = "TIMESTAMP")
    public LocalDateTime zeit;

    @jakarta.persistence.ManyToOne
    public Film film;

    @jakarta.persistence.ManyToOne
    public KinoSaal kinoSaal;

    @jakarta.persistence.OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    public Kontingent kontingent;

    protected Vorfuehrung() {
    }

    public Vorfuehrung(final String id, final LocalDateTime zeit, final Film film, final KinoSaal kinoSaal) {
        this(id, zeit, film, kinoSaal, Config.QUOTE_ONLINE);
    }

    public Vorfuehrung(final String id, final LocalDateTime zeit, final Film film, final KinoSaal kinoSaal, final int quoteOnline) {
        this.id = id;
        this.zeit = zeit;
        this.film = film;
        this.kinoSaal = kinoSaal;

        //TODO this validation should not be done here. If class is not final, Spotbugs complains. For DB entities use "nullable=false" and "optional=false" for DB checks. In domain classes check with jakarta.annotation NonNull? Also: Why is KinoSaal obligatory but not Film?
        if (kinoSaal == null) {
            throw new FlexinaleIllegalArgumentException("KinoSaal of Vorfuehrung " + this +
                    " must not be null");
        }
        //TODO this validation should not be done here but in KinoSaal itself
        if (kinoSaal.anzahlPlaetze == null) {
            throw new FlexinaleIllegalArgumentException("KinoSaal's Anzahl Plaetze of Vorfuehrung " + this +
                    " must not be null");
        }

        kontingent = new Kontingent("K" + id, kinoSaal.anzahlPlaetze, quoteOnline);
    }

    @Override
    public Id id() {
        return Identifiable.Id.of(id);
    }

    public int getGesamtkapazitaetVerkauf() {
        return kinoSaal.anzahlPlaetze;
    }

    public void verkaufeTickets(final Ticket.VerkaufsKanal verkaufsKanal, final int anzahlTickets) {
        switch (verkaufsKanal) {
            case ONLINE ->
                    kontingent.reduziereKontingent(Kontingent.KontingentType.ONLINE, anzahlTickets);
            case ZENTRAL ->
                    kontingent.reduziereKontingent(Kontingent.KontingentType.ZENTRAL, anzahlTickets);
            case KINOKASSE ->
                    kontingent.reduziereKontingent(Kontingent.KontingentType.KINOKASSE, anzahlTickets);
        }
    }

    public boolean ueberlapptMit(final Vorfuehrung other, final long puffer) {
        LocalDateTime begin = this.zeit;
        LocalDateTime beginOther = other.zeit;
        LocalDateTime end = begin.plusMinutes(this.film.dauerInMinuten);
        LocalDateTime endOther = beginOther.plusMinutes(other.film.dauerInMinuten);

        return ((begin.isBefore(endOther.plusMinutes(puffer)))
                && (end.isAfter(beginOther.minusMinutes(puffer))));
    }

    @Override
    public String toString() {
        return ToStringBuilder.reflectionToString(this, ToStringStyle.MULTI_LINE_STYLE);
    }

    @Override
    public boolean equals(final Object o) {
        if (this == o) {return true;}

        if (o == null || getClass() != o.getClass()) {return false;}

        Vorfuehrung that = (Vorfuehrung) o;

        return new EqualsBuilder()
                .append(id, that.id).append(zeit, that.zeit)
                .append(film, that.film)
                .append(kinoSaal, that.kinoSaal)
                .append(kontingent, that.kontingent)
                .isEquals();
    }

    @Override
    public int hashCode() {
        return new HashCodeBuilder(17, 37)
                .append(id).append(zeit).append(film).append(kinoSaal)
                .append(kontingent)
                .toHashCode();
    }
}


