package gaf3.core.services.user.entity

import com.fasterxml.jackson.annotation.JsonIgnore
import gaf3.core.jpa.GafTimestamp
import gaf3.core.jpa.annotation.WithIgnorePath
import gaf3.core.util.EncryptUtil
import javax.persistence.*
import javax.validation.constraints.NotEmpty

@Entity
@Table(name = "GAF_USER_ACCT")
class GafUserAccount : GafTimestamp() {
    @Id
    @Column(name = "USER_ID", length = 48)
    var userId: String? = null

    @NotEmpty
    @Column(name = "ACCOUNT", length = 48)
    var account: String? = null

    @WithIgnorePath
    @JsonIgnore
    @Column(name = "SECRET")
    var secret: String? = null

    @Column(name = "STATUS", length = 48)
    var status: String? = null

    @Column(name = "EXPIRED")
    var expired: Long = -1

    @Column(name = "REMARK")
    var remark: String? = null

    @JsonIgnore
    @Transient
    var unwrapPass: String? = null

    @PrePersist
    @PreUpdate
    fun cryptWrap() {
        secret = EncryptUtil.CryptWrap(secret)
    }

    @PostLoad
    @PostUpdate
    @PostPersist
    fun cryptUnwrap() {
        secret = EncryptUtil.CryptUnwrap(secret)
        unwrapPass = secret
    }
}