import {
  AfterLoad,
  BeforeInsert,
  Column,
  Entity,
  JoinColumn,
  ManyToOne,
  OneToMany,
  PrimaryGeneratedColumn,
  CreateDateColumn,
  UpdateDateColumn,
  DeleteDateColumn,
} from "typeorm"
import { Role } from "../../role/entities/role.entity"
import * as bcrypt from "bcrypt"
import { UserLogin } from "./user-login.entity"
import * as process from "node:process"
import { isEmpty } from "../../../utils/helpers"
import { UserOldPassword } from "./user-old-password.entity"
import { TeamMember } from "src/modules/team-member/entities/team_member.entity"
import { Notification } from "src/modules/notification/entities/notification.entity"

@Entity("users")
export class Auth {
  @PrimaryGeneratedColumn()
  id: number

  @Column({ type: "int", nullable: true })
  role_id: number

  @Column({ type: "int", nullable: true })
  team_member_id: number

  @Column({ type: "varchar", nullable: true })
  first_name: string

  @Column({ type: "varchar", nullable: true })
  last_name: string

  @Column({ type: "varchar", nullable: true })
  slug: string

  @Column({ type: "varchar", nullable: false })
  email: string

  @Column({ type: "varchar", nullable: true })
  password: string

  @Column({ type: "varchar", nullable: true })
  country_code: string

  @Column({ type: "varchar", nullable: true })
  contact_no: string

  @Column({ type: "varchar", nullable: true })
  profile_pic: string

  @Column({ type: "varchar", nullable: true })
  status: string

  @Column({ type: "smallint", default: 0 })
  invalid_password_attempt: number

  @Column({ type: "bigint", nullable: true })
  account_locked_at: number | null

  @Column({ type: "timestamp", nullable: true })
  last_login_at: Date

  @Column({ type: "varchar", nullable: true })
  otp: string

  @Column({ type: "timestamp", nullable: true })
  otp_expires_at: Date

  @Column({ type: "boolean", default: false })
  is_document: boolean

  @DeleteDateColumn({ type: "timestamp", nullable: true })
  deleted_at: Date

  @CreateDateColumn({
    type: "timestamp",
    nullable: true,
    default: () => "NOW()",
  })
  created_at: Date

  @UpdateDateColumn({
    type: "timestamp",
    nullable: true,
    default: () => "NOW()",
  })
  updated_at: Date

  @ManyToOne(() => Role, (role) => role.users)
  @JoinColumn({ name: "role_id" })
  role: Role

  @ManyToOne(() => TeamMember, (teamMember) => teamMember.users)
  @JoinColumn({ name: "team_member_id" })
  team_members: TeamMember[]

  @OneToMany(() => UserLogin, (userLogins) => userLogins.user)
  userLogins: UserLogin[]

  @OneToMany(() => Notification, (notification) => notification.user)
  userNotifications: Notification[]

  @OneToMany(() => UserOldPassword, (oldPassword) => oldPassword.user)
  userOldPasswords: UserOldPassword[]

  @BeforeInsert()
  async setPassword(password: string) {
    if (!isEmpty(this.password)) {
      const salt = await bcrypt.genSalt()
      this.password = await bcrypt.hash(password || this.password, salt)
    }
  }
  //extra fields
  access_token?: string

  @Column({ nullable: true })
  password_reset_token: string

  @Column({ type: "bigint", nullable: true })
  password_reset_token_generated_at: number

  @AfterLoad()
  async profilePict() {
    if (isEmpty(this.profile_pic)) {
      this.profile_pic = `${process.env.BACK_URL}/static-images/png/img-plceholder@2x.png`
    } else {
      this.profile_pic = `${process.env.BACK_URL}/` + this.profile_pic
    }
  }
}
