import {
  Entity,
  PrimaryGeneratedColumn,
  Column,
  OneToOne,
  JoinColumn,
  AfterLoad,
} from "typeorm"
import { BaseEntity } from "../../common/entities/base.entity"
import { Employee } from "./employee.entity"
import { isEmpty } from "class-validator"

@Entity("employee_information")
export class EmployeeInformation extends BaseEntity {
  @PrimaryGeneratedColumn()
  id: number

  @Column({ type: "int", nullable: false })
  employee_id: number

  @Column({ type: "text", nullable: false })
  address: string

  @Column({ type: "text", nullable: true })
  city: string

  @Column({ type: "text", nullable: true })
  state: string

  @Column({ type: "varchar", length: 10, nullable: false })
  gender: string

  @Column({ type: "varchar", length: 20, nullable: false })
  alternate_mobile_number: string

  @Column({ type: "varchar", length: 255, nullable: true })
  personal_email: string

  @Column({ type: "date", nullable: true })
  date_of_birth: Date

  @Column({ type: "varchar", length: 20, nullable: true })
  marital_status: string

  @Column({ type: "varchar", length: 10, nullable: true })
  blood_group: string

  @Column({ type: "varchar", length: 255, nullable: true })
  parents_name: string

  @Column({ type: "varchar", length: 20, nullable: true })
  parents_contact_number: string

  @Column({ type: "varchar", length: 255, nullable: true })
  emergency_contact_name: string

  @Column({ type: "varchar", length: 255, nullable: true })
  emergency_contact_relationship: string

  @Column({ type: "varchar", length: 20, nullable: true })
  emergency_contact_number: string

  @Column({ type: "text", nullable: true })
  permanent_address: string

  @Column({ type: "varchar", length: 10, nullable: true })
  pincode: string

  @Column({ type: "varchar", length: 255, nullable: true })
  native_place: string

  @Column({ type: "decimal", precision: 4, scale: 1, nullable: true })
  total_work_experience_years: number

  @Column({ type: "varchar", length: 255, nullable: true })
  previous_company_name: string

  @Column({ type: "varchar", length: 255, nullable: true })
  previous_designation: string

  @Column({ type: "decimal", precision: 10, scale: 2, nullable: true })
  last_salary: number

  @Column({ type: "varchar", length: 255, nullable: true })
  education_qualification: string

  @Column({ type: "varchar", length: 255, nullable: true })
  bank_name: string

  @Column({ type: "varchar", length: 50, nullable: true })
  account_number: string

  @Column({ type: "varchar", length: 50, nullable: true })
  ifsc_code: string

  @Column({ type: "varchar", length: 255, nullable: true })
  account_holder_name: string

  @Column({ type: "varchar", length: 20, nullable: true })
  aadhar_card: string

  @Column({ type: "varchar", length: 20, nullable: true })
  pan_card_number: string

  @Column({ type: "varchar", length: 500, nullable: true })
  uan: string

  @Column({ type: "varchar", length: 500, nullable: true })
  resume_url: string

  @Column({ type: "varchar", length: 500, nullable: true })
  offer_letter: string

  @Column({ type: "varchar", length: 500, nullable: true })
  appointment_letter: string

  @Column({ type: "varchar", length: 500, nullable: true })
  id_proof: string

  @Column({ type: "varchar", length: 500, nullable: true })
  address_proof: string

  @Column({ type: "varchar", length: 500, nullable: true })
  education_certificates: string

  @OneToOne(() => Employee, (employee) => employee.employeeInformation)
  @JoinColumn({ name: "employee_id" })
  employee: Employee

  @AfterLoad()
  setDocumentUrls() {
    const base = process.env.BASE_URL ?? ""
    if (!isEmpty(this.resume_url)) this.resume_url = base + this.resume_url
    if (!isEmpty(this.offer_letter))
      this.offer_letter = base + this.offer_letter
    if (!isEmpty(this.appointment_letter))
      this.appointment_letter = base + this.appointment_letter
    if (!isEmpty(this.id_proof)) this.id_proof = base + this.id_proof
    if (!isEmpty(this.address_proof))
      this.address_proof = base + this.address_proof
    if (!isEmpty(this.education_certificates))
      this.education_certificates = base + this.education_certificates
  }
}
