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

import de.accso.flexinale.common.shared_kernel.Identifiable;
import de.accso.flexinale.common.shared_kernel.Versionable;
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;

import java.io.Serializable;

@jakarta.persistence.Entity(name = "KinoSaal")
public class KinoSaalEntity implements Identifiable, Versionable, Serializable {
    @jakarta.persistence.Id
    public String id;  // primary key

    @jakarta.persistence.Column
    public String name;

    @jakarta.persistence.Column
    public Integer anzahlPlaetze = 0;

    @jakarta.persistence.ManyToOne
    @jakarta.persistence.JoinColumn(name = "kino", nullable = false)
    public KinoEntity kino;

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

    protected KinoSaalEntity() {
    }

    public KinoSaalEntity(final String id, final Integer version,
                          final String name, final Integer anzahlPlaetze, final KinoEntity kino) {
        this.id = id;
        this.version = version;
        this.name = name;
        this.anzahlPlaetze = anzahlPlaetze;
        this.kino = kino;
    }

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

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

    @Override
    public String toString() {
        return "KinoSaalEntity{" +
                "id='" + id + '\'' +
                ", name='" + name + '\'' +
                ", anzahlPlaetze=" + anzahlPlaetze +
                (kino != null ? ", kino.id=" + kino.id : ", kino=null") +
                ", version=" + version +
                '}';
    }

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

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

        KinoSaalEntity that = (KinoSaalEntity) o;

        EqualsBuilder builder = new EqualsBuilder()
                .append(id, that.id).append(version, that.version)
                .append(name, that.name).append(anzahlPlaetze, that.anzahlPlaetze);
        if (kino != null) {
            builder.append(kino.id, that.kino.id); // do not check kino but only its id (otherwise Stackoverflow error)
        }
        return builder.isEquals();
    }

    @Override
    public int hashCode() {
        HashCodeBuilder builder = new HashCodeBuilder(17, 37)
                .append(id).append(version)
                .append(name).append(anzahlPlaetze);
        if (kino != null) {
            builder.append(kino.id); // do not check kino but only its id (otherwise Stackoverflow error)
        }
        return builder.toHashCode();
    }
}
