package de.accso.flexinale.infrastructure.persistence;

import de.accso.flexinale.shared_kernel.DateTimeHelper;
import de.accso.flexinale.shared_kernel.FlexinaleIllegalArgumentException;
import de.accso.flexinale.shared_kernel.Identifiable;
import de.accso.flexinale.shared_kernel.Versionable;
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(name = "Vorfuehrung")
public final class VorfuehrungEntity implements Identifiable, Versionable, Serializable { // TODO is only final as otherwise Spotbugs
    @jakarta.persistence.Id
    public String id;  // primary key

    @jakarta.persistence.Column
    public Long zeit;  // save EPOCH seconds instead of Date/Time (Timezone is UTC)

    @jakarta.persistence.ManyToOne
    public FilmEntity film;

    @jakarta.persistence.ManyToOne
    public KinoSaalEntity kinoSaal;

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

    @jakarta.persistence.Version
    private Integer version = 0;

    protected VorfuehrungEntity() {
    }

    public VorfuehrungEntity(final String id, final Integer version,
                             final LocalDateTime zeit,
                             final FilmEntity film,
                             final KinoSaalEntity kinoSaal,
                             final KontingentEntity kontingent) {
        this.id = id;
        this.version = version;
        this.zeit = DateTimeHelper.toEpochSeconds(zeit);
        this.film = film;
        this.kinoSaal = kinoSaal;
        this.kontingent = kontingent;

        //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");
        }
        if (kontingent == null) {
            throw new FlexinaleIllegalArgumentException("Kontingent of Vorfuehrung " + this +
                    " must not be null");
        }
    }

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

    @Override
    public Version version() {
        return Versionable.Version.of(version);
    }

    @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;}

        VorfuehrungEntity that = (VorfuehrungEntity) o;

        return new EqualsBuilder()
                .append(id, that.id).append(version, that.version)
                .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(version)
                .append(zeit).append(film).append(kinoSaal)
                .append(kontingent)
                .toHashCode();
    }
}


