package com.dbnt.kcscbackend.auth.entity;

import com.dbnt.kcscbackend.config.common.BoardParams;
import com.fasterxml.jackson.annotation.JsonIgnore;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.crypto.password.Pbkdf2PasswordEncoder;

import javax.persistence.*;
import javax.validation.constraints.Email;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Pattern;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

@Getter
@Setter
@Entity
@NoArgsConstructor
@DynamicInsert
@DynamicUpdate
@Table(name = "user_info")
public class UserInfo extends BoardParams implements UserDetails{
  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  @Column(name = "user_seq")
  private Integer userSeq;
  @Column(name = "user_id")
  @Pattern(regexp = "^[a-zA-Z]{1}[a-zA-Z0-9_]{4,11}$")
  @NotBlank(message = "아이디를 입력해주세요.")
  private String userId;
  @Column(name = "password")
  private String password;
  @Column(name = "email")
  @Email(message = "이메일 형식에 맞지 않습니다.")
  @Schema(description = "이메일주소")
  @NotBlank(message = "이메일을 입력해주세요.")
  private String email;
  @Column(name = "user_se")
  private String userSe;
  @Column(name = "user_nm")
  @NotBlank(message = "이름을 입력해주세요.")
  private String userNm;
  @Column(name = "phone_num")
  @NotBlank(message = "연락처를 입력해주세요.")
  private String phoneNum;
  @Column(name = "user_role")
  private String userRole;
  @Column(name = "status")
  private String status;
  @Column(name = "frst_crt_dt")
  @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
  private LocalDateTime frstCrtDt;
  @Column(name = "last_chg_id")
  private String lastChgId;
  @Column(name = "last_chg_dt")
  @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
  private LocalDateTime lastChgDt;

  @Transient
  private String passwordChk;
  @Transient
  private String statusValue;

  @Override
  @JsonIgnore
  public Collection<? extends GrantedAuthority> getAuthorities() {
    Set<GrantedAuthority> roles = new HashSet<>();
    for (String role : userSe.split(",")) {
        roles.add(new SimpleGrantedAuthority(role));
    }
    return roles;
  }

  @Override
  public String getUsername() {
      return userId;
  }

  @Override
  public boolean isAccountNonExpired() {
      return true;
  }

  @Override
  public boolean isAccountNonLocked() {
      return true;
  }

  @Override
  public boolean isCredentialsNonExpired() {
      return true;
  }

  @Override
  @JsonIgnore
  public boolean isEnabled() {
      return getStatus().equals("USE_ST");
  }



  public String convertPassword(String password){
    Pbkdf2PasswordEncoder passwordEncoder = new Pbkdf2PasswordEncoder();
    return passwordEncoder.encode(password);
  }

}
