import {
  Column,
  CreateDateColumn,
  DeleteDateColumn,
  Entity,
  JoinColumn,
  ManyToOne,
  PrimaryGeneratedColumn,
  UpdateDateColumn,
  AfterLoad,
} from "typeorm"
import { CustomerTreatmentPlan } from "./customer_treatment_plan.entity"

@Entity("customer_treatment_plan_images")
export class CustomerTreatmentPlanImage {
  @PrimaryGeneratedColumn()
  id: number

  @Column({ type: "int" })
  treatment_plan_id: number

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

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

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

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

  @ManyToOne(() => CustomerTreatmentPlan, (plan) => plan.images, {
    onDelete: "CASCADE",
  })
  @JoinColumn({ name: "treatment_plan_id" })
  treatment_plan: CustomerTreatmentPlan

  @AfterLoad()
  async planImage() {
    if (this.plan_image) {
      this.plan_image =
        process.env.FILE_UPLOAD === "Local"
          ? `${process.env.BACK_URL}/${this.plan_image}`
          : `${process.env.CLOUDFLARE_R2_PUBLIC_URL}/${this.plan_image}`
    }
  }
}
