package marketing.heyday.hartmann.fotodocumentation.core.model;

import java.util.HashSet;
import java.util.Set;

import org.apache.commons.lang.builder.HashCodeBuilder;

import jakarta.persistence.*;

/**
 * 
 * 
 * <p>Copyright: Copyright (c) 2024</p>
 * <p>Company: heyday Marketing GmbH</p>
 * @author <a href="mailto:p.verboom@heyday.marketing">Patrick Verboom</a>
 * @version 1.0
 * 
 * created: 19 Jan 2026
 */

@Entity
@Table(name = "x_user")
@NamedQuery(name = User.BY_USERNAME, query = "select u from User u where u.username like :username")
public class User extends AbstractDateEntity {
	private static final long serialVersionUID = 1L;
	public static final String SEQUENCE = "user_seq";
	public static final String BY_USERNAME = "user.byUsername";
	public static final String PARAM_USERNAME = "username";

	@Id
	@Column(name = "user_id", length = 22)
	@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = SEQUENCE)
	@SequenceGenerator(name = SEQUENCE, sequenceName = SEQUENCE, allocationSize = 1)
	private Long userId;

	@Column(name = "username", unique = true, nullable = false)
	private String username;

	@Column(nullable = false)
	private String password;

	private String salt;

	@Column(name = "title")
	private String title;

	@Column(nullable = false)
	private String firstname;

	@Column(nullable = false)
	private String lastname;

	@Column(nullable = false)
	private String email;

	@ManyToMany
	@JoinTable(name = "user_to_right", joinColumns = { @JoinColumn(name = "user_id_fk") }, inverseJoinColumns = { @JoinColumn(name = "right_id_fk") })
	private Set<Right> rights = new HashSet<>();

	public Long getUserId() {
		return userId;
	}

	public void setUserId(Long userId) {
		this.userId = userId;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getSalt() {
		return salt;
	}

	public void setSalt(String salt) {
		this.salt = salt;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getFirstname() {
		return firstname;
	}

	public void setFirstname(String firstname) {
		this.firstname = firstname;
	}

	public String getLastname() {
		return lastname;
	}

	public void setLastname(String lastname) {
		this.lastname = lastname;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public Set<Right> getRights() {
		return rights;
	}

	public void setRights(Set<Right> rights) {
		this.rights = rights;
	}

	@Override
	public int hashCode() {
		return new HashCodeBuilder().append(username).toHashCode();
	}

	@Override
	public boolean equals(Object obj) {
		if (obj == null || this.getClass() != obj.getClass()) {
			return false;
		}
		return this.username.equals(((User) obj).getUsername());
	}

	public static class Builder {
		private User instance = new User();

		public Builder email(String email) {
			instance.setEmail(email);
			return this;
		}

		public Builder firstname(String firstname) {
			instance.setFirstname(firstname);
			return this;
		}

		public Builder lastname(String lastname) {
			instance.setLastname(lastname);
			return this;
		}

		public Builder title(String title) {
			instance.setTitle(title);
			return this;
		}

		public Builder username(String username) {
			instance.setUsername(username);
			return this;
		}

		public Builder password(String password) {
			instance.setPassword(password);
			return this;
		}

		public User build() {
			return instance;
		}
	}
}
