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

import java.util.Date;

import com.fasterxml.jackson.annotation.JsonIgnore;

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: 21 Jan 2026
 */
@Entity
@Table(name = "jwt_refresh_token")
@NamedQuery(name = JwtRefreshToken.FIND_BY_HASH_REVOKE_NULL, query = "SELECT t FROM JwtRefreshToken t WHERE t.tokenHash = :hash AND t.revokedAt IS NULL")
@NamedQuery(name = JwtRefreshToken.FIND_BY_HASH, query = "SELECT t FROM JwtRefreshToken t WHERE t.tokenHash = :hash")
@NamedQuery(name = JwtRefreshToken.REVOKE_ALL_USER, query = "UPDATE JwtRefreshToken t SET t.revokedAt = :date WHERE t.user.userId = :userId AND t.revokedAt IS NULL")
public class JwtRefreshToken extends AbstractEntity {
	private static final long serialVersionUID = 1L;
	public static final String SEQUENCE = "jwt_refresh_token_seq";
	public static final String FIND_BY_HASH_REVOKE_NULL = "JwtRefreshToken.forHashRevokeNull";
	public static final String FIND_BY_HASH = "JwtRefreshToken.forHash";
	public static final String REVOKE_ALL_USER = "JwtRefreshToken.revokeAllUser";
	public static final String PARAM_HASH = "hash";
	public static final String PARAM_USER_ID = "userId";
	public static final String PARAM_DATE = "date";

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

	@Column(name = "token_hash", nullable = false)
	private String tokenHash;

	@Column(name = "device_info", nullable = false)
	private String deviceInfo;

	@Column(name = "ip_address", nullable = false)
	private String ipAddress;

	@Temporal(TemporalType.TIMESTAMP)
	@Column(name = "issued_at", nullable = false)
	@JsonIgnore
	private Date issuedAt;

	@Temporal(TemporalType.TIMESTAMP)
	@Column(name = "expires_at", nullable = false)
	@JsonIgnore
	private Date expiresAt;

	@Temporal(TemporalType.TIMESTAMP)
	@Column(name = "revoked_at", nullable = false)
	@JsonIgnore
	private Date revokedAt;

	@Temporal(TemporalType.TIMESTAMP)
	@Column(name = "last_used_at", nullable = false)
	@JsonIgnore
	private Date lastUsedAt;

	@ManyToOne(fetch = FetchType.EAGER)
	@JoinColumn(name = "user_id_fk", nullable = true)
	private User user;

	public Long getJwsRefreshTokenId() {
		return jwsRefreshTokenId;
	}

	public void setJwsRefreshTokenId(Long jwsRefreshTokenId) {
		this.jwsRefreshTokenId = jwsRefreshTokenId;
	}

	public String getTokenHash() {
		return tokenHash;
	}

	public void setTokenHash(String tokenHash) {
		this.tokenHash = tokenHash;
	}

	public String getDeviceInfo() {
		return deviceInfo;
	}

	public void setDeviceInfo(String deviceInfo) {
		this.deviceInfo = deviceInfo;
	}

	public String getIpAddress() {
		return ipAddress;
	}

	public void setIpAddress(String ipAddress) {
		this.ipAddress = ipAddress;
	}

	public Date getIssuedAt() {
		return issuedAt;
	}

	public void setIssuedAt(Date issuedAt) {
		this.issuedAt = issuedAt;
	}

	public Date getExpiresAt() {
		return expiresAt;
	}

	public void setExpiresAt(Date expiresAt) {
		this.expiresAt = expiresAt;
	}

	public Date getRevokedAt() {
		return revokedAt;
	}

	public void setRevokedAt(Date revokedAt) {
		this.revokedAt = revokedAt;
	}

	public Date getLastUsedAt() {
		return lastUsedAt;
	}

	public void setLastUsedAt(Date lastUsedAt) {
		this.lastUsedAt = lastUsedAt;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

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

		public Builder tokenHash(String tokenHash) {
			instance.setTokenHash(tokenHash);
			return this;
		}

		public Builder deviceInfo(String deviceInfo) {
			instance.setDeviceInfo(deviceInfo);
			return this;
		}

		public Builder ipAddress(String ipAddress) {
			instance.setIpAddress(ipAddress);
			return this;
		}

		public Builder expiresAt(Date expiresAt) {
			instance.setExpiresAt(expiresAt);
			return this;
		}

		public Builder issuedAt(Date issuedAt) {
			instance.setIssuedAt(issuedAt);
			return this;
		}

		public Builder revokedAt(Date revokedAt) {
			instance.setRevokedAt(revokedAt);
			return this;
		}

		public Builder lastUsedAt(Date lastUsedAt) {
			instance.setLastUsedAt(lastUsedAt);
			return this;
		}

		public Builder user(User user) {
			instance.setUser(user);
			return this;
		}

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