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

import de.accso.flexinale.common.shared_kernel.Identifiable;
import de.accso.flexinale.common.shared_kernel.Mergeable;
import de.accso.flexinale.common.shared_kernel.Versionable;
import jakarta.persistence.EnumType;
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 org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

import java.io.Serializable;

@jakarta.persistence.Entity(name="Benutzer")
@SuppressWarnings("unused")
public class BenutzerEntity implements Identifiable, Versionable, Mergeable<BenutzerEntity>, Serializable {

    @SuppressWarnings("unused")
    public enum Rolle { // Spring Security needs "ROLE_" as default prefix! Do not change.
        ROLE_UNBEKANNT,
        ROLE_BESUCHER,
        ROLE_ADMIN
    }

    @jakarta.persistence.Id
    public String id;  // primary key

    @jakarta.persistence.Column(unique = true)
    public String login;

    @jakarta.persistence.Column(unique = true)
    public String emailAdresse;

    @jakarta.persistence.Column
    public String name;

    @jakarta.persistence.Column
    public String vorname;

    @jakarta.persistence.Column
    @jakarta.persistence.Enumerated(EnumType.STRING)
    public Rolle rolle;

    @jakarta.persistence.Column
    private String passwort;

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

    protected BenutzerEntity() {
    }

    public BenutzerEntity(final String id,
                          final String login, final String emailAdresse, final String name, final String vorname) {
        this(id, Versionable.unknownVersion().version(), login, emailAdresse, name, vorname, Rolle.ROLE_UNBEKANNT);
    }

    public BenutzerEntity(final String id, final Integer version,
                          final String login, final String emailAdresse, final String name, final String vorname) {
        this(id, version, login, emailAdresse, name, vorname, Rolle.ROLE_UNBEKANNT);
    }

    public BenutzerEntity(final String id,
                          final String login, final String emailAdresse, final String name, final String vorname, Rolle rolle) {
        this(id, Versionable.unknownVersion().version(), login, emailAdresse, name, vorname, rolle);
    }

    public BenutzerEntity(final String id, final Integer version,
                          final String login, final String emailAdresse, final String name, final String vorname, Rolle rolle) {
        this.id = id;
        this.version = version;
        this.login = login;
        this.emailAdresse = emailAdresse;
        this.name = name;
        this.vorname = vorname;
        this.rolle = rolle;
    }

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

    @Override
    public BenutzerEntity merge(final BenutzerEntity newData) {
        return new BenutzerEntity(this.id, this.version,
                newData.login, newData.emailAdresse, newData.name, newData.vorname, newData.rolle);
    }

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

    public String getPasswort() {
        return passwort;
    }

    public void encryptAndSetPassword(final String passwort) {
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        this.passwort = encoder.encode(passwort);
    }

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

        BenutzerEntity that = (BenutzerEntity) o;

        return new EqualsBuilder()
                .append(id, that.id).append(version, that.version)
                .append(login, that.login)
                .append(emailAdresse, that.emailAdresse)
                .append(name, that.name)
                .append(vorname, that.vorname).isEquals();
    }

    @Override
    public int hashCode() {
        return new HashCodeBuilder(17, 37)
                .append(id).append(version)
                .append(login)
                .append(emailAdresse).append(name).append(vorname).toHashCode();
    }
}
