package com.spring.domain.user.entity;

import java.util.UUID;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.ForeignKey;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.MapsId;
import javax.persistence.OneToOne;
import javax.persistence.Table;

import com.spring.common.jpa.AuditEntity;

import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Entity
@Table(name = "AGENT_USER_TOKEN")
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class AgentUserToken extends AuditEntity {
    
    @Id
    @Column(name = "ID", nullable = false)
    private UUID id;
    
    @OneToOne(fetch = FetchType.LAZY)
    @MapsId
    @JoinColumn(name = "ID", referencedColumnName = "ID", columnDefinition = "BINARY(16)", foreignKey = @ForeignKey(name = "FK_AGENT_USER_TOKEN"))
    private AgentUser agentUser;

    @Column(name = "REFRESH_TOKEN", nullable = false)
    private String refreshToken;

    @Builder
    public AgentUserToken(AgentUser agentUser, String refreshToken) {
        this.agentUser = agentUser;
        this.refreshToken = refreshToken;
    }

    public void updateRefreshToken(String refreshToken) {
        this.refreshToken = refreshToken;
    }

    public boolean validateRefreshToken(String refreshToken) {
        return this.refreshToken.equals(refreshToken);
    }

}
