import { FleetManagement } from "src/modules/fleet-management/entities/fleet-management.entity"
import {
  Entity,
  PrimaryGeneratedColumn,
  Column,
  ManyToOne,
  JoinColumn,
  CreateDateColumn,
  UpdateDateColumn,
  DeleteDateColumn,
  AfterLoad,
} from "typeorm"

@Entity("vehicle_insurances")
export class VehicleInsurance {
  @PrimaryGeneratedColumn()
  id: number

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

  @Column({ type: "varchar" })
  policy_number: string

  @Column({ type: "varchar" })
  insurance_provider: string

  @Column({ type: "varchar" })
  coverage_type: string

  @Column({ type: "date" })
  start_date: Date

  @Column({ type: "date" })
  end_date: Date

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

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

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

  @ManyToOne(() => FleetManagement, (fleet) => fleet.vehicleInsurances)
  @JoinColumn({ name: "fleet_id" })
  fleet: FleetManagement

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

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

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

  @AfterLoad()
  async profilePict() {
    if (!this.document_file) {
      this.document_file = ""
    } else {
      this.document_file =
        process.env.FILE_UPLOAD === "Local"
          ? `${process.env.BACK_URL}/` + this.document_file
          : `${process.env.CLOUDFLARE_R2_PUBLIC_URL}/${this.document_file}`
    }
  }
}
