import {
  Entity,
  PrimaryGeneratedColumn,
  Column,
  JoinColumn,
  OneToOne,
  AfterLoad,
} from "typeorm"
import { BaseEntity } from "../../common/entities/base.entity"
import { Company } from "./company.entity"
import { isEmpty } from "../../../utils/helpers"
import * as process from "node:process"

@Entity("company_profiles")
export class CompanyProfile extends BaseEntity {
  @PrimaryGeneratedColumn()
  id: number

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

  @Column({ type: "varchar", length: 255, nullable: false })
  first_name: string

  @Column({ type: "varchar", length: 255, nullable: false })
  last_name: string

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

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

  @Column({ type: "varchar", length: 10, nullable: true })
  country_code: number

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

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

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

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

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

  @OneToOne(() => Company)
  @JoinColumn({ name: "company_id" })
  company: Company

  // ====== LOGO FALLBACK ======
  @AfterLoad()
  async setLogoUrl() {
    if (isEmpty(this.logo)) {
      this.logo = `${process.env.BASE_URL}static-images/png/img-plceholder@2x.png`
    } else {
      this.logo = process.env.BASE_URL + this.logo
    }
  }
}
