package de.accso.flexinale.infrastructure.persistence;

import de.accso.flexinale.shared_kernel.Identifiable;
import de.accso.flexinale.shared_kernel.Versionable;
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;

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

    @jakarta.persistence.Column
    public String titel;

    @jakarta.persistence.Column
    public String imdbUrl;

    @jakarta.persistence.Column
    public Integer dauerInMinuten;

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

    protected FilmEntity() {
    }

    public FilmEntity(final String id, final Integer version,
                      final String titel, final String imdbUrl, final Integer dauerInMinuten) {
        this.id = id;
        this.version = version;
        this.titel = titel;
        this.imdbUrl = imdbUrl;
        this.dauerInMinuten = dauerInMinuten;
    }

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

        FilmEntity that = (FilmEntity) o;

        return new EqualsBuilder()
                .append(id, that.id).append(version, that.version)
                .append(titel, that.titel)
                .append(imdbUrl, that.imdbUrl)
                .append(dauerInMinuten, that.dauerInMinuten)
                .isEquals();
    }

    @Override
    public int hashCode() {
        return new HashCodeBuilder(17, 37)
                .append(id).append(version)
                .append(titel).append(imdbUrl).append(dauerInMinuten).toHashCode();
    }
}
