package com.kamco.cd.kamcoback.postgres.entity;

import com.kamco.cd.kamcoback.common.enums.StatusType;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import java.time.ZonedDateTime;
import java.util.Objects;
import java.util.UUID;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.ColumnDefault;

@Getter
@Setter
@Entity
@Table(name = "tb_member")
public class MemberEntity {

  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  @Column(name = "id", nullable = false)
  private Long id;

  @NotNull
  @ColumnDefault("gen_random_uuid()")
  @Column(name = "uuid", nullable = false)
  private UUID uuid = UUID.randomUUID();

  @Size(max = 50)
  @NotNull
  @Column(name = "user_role", nullable = false, length = 50)
  private String userRole;

  @Size(max = 50)
  @NotNull
  @Column(name = "user_id", nullable = false, length = 50)
  private String userId;

  @Size(max = 50)
  @NotNull
  @Column(name = "employee_no", nullable = false, length = 50)
  private String employeeNo;

  @Size(max = 100)
  @NotNull
  @Column(name = "name", nullable = false, length = 100)
  private String name;

  @Size(max = 255)
  @NotNull
  @Column(name = "password", nullable = false)
  private String password;

  @Size(max = 20)
  @Column(name = "status", length = 20)
  private String status = StatusType.PENDING.getId();

  @NotNull
  @ColumnDefault("now()")
  @Column(name = "created_dttm", nullable = false)
  private ZonedDateTime createdDttm = ZonedDateTime.now();

  @ColumnDefault("now()")
  @Column(name = "updated_dttm")
  private ZonedDateTime updatedDttm = ZonedDateTime.now();

  @Column(name = "first_login_dttm")
  private ZonedDateTime firstLoginDttm;

  @Column(name = "last_login_dttm")
  private ZonedDateTime lastLoginDttm;

  @Column(name = "login_fail_count")
  @ColumnDefault("0")
  private Integer loginFailCount = 0;

  @Column(name = "rgstr_uid")
  private Long rgstrUidl;

  @Column(name = "updtr_uid")
  private Long updtrUid;

  @Column(name = "status_chg_dttm")
  private ZonedDateTime statusChgDttm;

  @Column(name = "pwd_reset_yn")
  private Boolean pwdResetYn;

  public void changeStatus(String newStatus) {
    // 같은 값 보내도 무시
    if (Objects.equals(this.status, newStatus)) {
      return;
    }
    this.status = newStatus;
    this.statusChgDttm = ZonedDateTime.now();
  }
}
