package gc.mda.kcg.permission;

import jakarta.persistence.*;
import lombok.*;
import org.hibernate.annotations.JdbcTypeCode;
import org.hibernate.type.SqlTypes;

import java.time.OffsetDateTime;
import java.util.UUID;

@Entity
@Table(name = "auth_user_role", schema = "kcg")
@IdClass(UserRoleId.class)
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class UserRole {

    @Id
    @JdbcTypeCode(SqlTypes.UUID)
    @Column(name = "user_id")
    private UUID userId;

    @Id
    @Column(name = "role_sn")
    private Long roleSn;

    @Column(name = "granted_at", nullable = false)
    private OffsetDateTime grantedAt;

    @JdbcTypeCode(SqlTypes.UUID)
    @Column(name = "granted_by")
    private UUID grantedBy;

    @PrePersist
    void prePersist() {
        if (grantedAt == null) grantedAt = OffsetDateTime.now();
    }
}
