package gc.mda.kcg.auth;

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_login_hist", schema = "kcg")
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class LoginHistory {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "hist_sn")
    private Long histSn;

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

    @Column(name = "user_acnt", length = 50)
    private String userAcnt;

    @Column(name = "login_dtm", nullable = false)
    private OffsetDateTime loginDtm;

    @Column(name = "login_ip", length = 45)
    private String loginIp;

    @Column(name = "user_agent", columnDefinition = "text")
    private String userAgent;

    @Column(name = "result", nullable = false, length = 20)
    private String result;  // SUCCESS, FAILED, LOCKED

    @Column(name = "fail_reason", length = 255)
    private String failReason;

    @Column(name = "auth_provider", length = 20)
    private String authProvider;

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