package de.accso.flexinale.infrastructure.persistence;

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.util.HashSet;
import java.util.Set;

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

    @jakarta.persistence.Column
    public String name;

    @jakarta.persistence.Column
    public String adresse;

    @jakarta.persistence.Column
    public String emailAdresse;

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

    @jakarta.persistence.OneToMany(mappedBy = "kino", fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    public Set<KinoSaalEntity> kinoSaele = new HashSet<>();

    protected KinoEntity() {
    }

    public KinoEntity(final String id, final Integer version,
                      final String name, final String adresse, final String emailAdresse) {
        this.id = id;
        this.version = version;
        this.name = name;
        this.adresse = adresse;
        this.emailAdresse = emailAdresse;
    }

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

        KinoEntity that = (KinoEntity) o;

        return new EqualsBuilder()
                .append(id, that.id).append(version, that.version)
                .append(name, that.name)
                .append(adresse, that.adresse)
                .append(emailAdresse, that.emailAdresse)
                .append(kinoSaele.stream().toList(), that.kinoSaele.stream().toList()) // need to use list, as Set does not support deep equals
                .isEquals();
    }

    @Override
    public int hashCode() {
        return new HashCodeBuilder(17, 37)
                .append(id).append(version)
                .append(name).append(adresse)
                .append(emailAdresse)
                .append(kinoSaele.stream().toList()) // need to use list, as Set does not support deep equals
                .toHashCode();
    }
}
