package de.accso.flexinale.data.model;

import de.accso.flexinale.shared_kernel.Identifiable;
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.Collection;
import java.util.HashSet;
import java.util.Set;

@jakarta.persistence.Entity
public class Kino implements Identifiable, 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.OneToMany(mappedBy = "kino", fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    public Set<KinoSaal> kinoSaele = new HashSet<>();

    protected Kino() {
    }

    public Kino(final String id) {
        this.id = id;
    }

    public Kino(final String id, final String name, final String adresse, final String emailAdresse, final Set<KinoSaal> kinoSaele) {
        this.id = id;
        this.name = name;
        this.adresse = adresse;
        this.emailAdresse = emailAdresse;
        this.kinoSaele = kinoSaele;
    }

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

    public void addSaele(final Collection<KinoSaal> saele) {
        kinoSaele.addAll(saele);
    }

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

        Kino that = (Kino) o;

        return new EqualsBuilder()
                .append(id, that.id)
                .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(name).append(adresse).append(emailAdresse)
                .append(kinoSaele.stream().toList()) // need to use list, as Set does not support deep equals
                .toHashCode();
    }
}
