import {
  Entity,
  Column,
  PrimaryGeneratedColumn,
  ManyToOne,
  CreateDateColumn,
  JoinColumn,
} from 'typeorm';
import { NewCertificate } from './new-certificate.entity';
// ❌ REMOVE: import { Template } from './../../template/entities/template.entity';

@Entity('certificate_version_history')
export class CertificateVersionHistory {
  @PrimaryGeneratedColumn()
  id: number;

  @ManyToOne(() => NewCertificate)
  @JoinColumn({ name: 'certificate_id' })
  certificate: NewCertificate;

  /** COMPLETE SNAPSHOT **/
  @Column()
  certificate_no: string;

  @Column()
  standard_name: string;

  @Column()
  standard_short: string;

  @Column()
  company_name_snapshot: string;

  @Column()
  address_snapshot: string;

  @Column()
  city_snapshot: string;

  @Column()
  country_snapshot: string;

  @Column()
  ea_codes: string;

  // ✅ NEW — cert type
  @Column({ nullable: true })
  cert_type: string;

  @Column('text')
  scope_of_work: string;

  @Column({ type: 'date' })
  originally_registered: Date;

  @Column({ type: 'date' })
  issue_date: Date;

  @Column({ type: 'date' })
  expire_date: Date;

  @Column()
  status: string;

  // ❌ REMOVED — pdf_url
  // @Column()
  // pdf_url: string;

  // ✅ NEW — scan pdf url snapshot
  @Column({ type: 'varchar', length: 500, nullable: true })
  scan_pdf_url: string;

  @Column()
  qrcode_token: string;

  // ✅ NEW — who made the change
  @Column({ type: 'int', nullable: true })
  changed_by: number;

  // ✅ NEW — reason for this version (issued / renewed / edited / revoked / scan_attached)
  @Column({ type: 'varchar', length: 100, nullable: true })
  change_reason: string;
  @Column({ type: 'date', nullable: true })
  surveillance_audit_due: Date | null;

  @Column({ type: 'date', nullable: true })
  recertification_due: Date | null;
  @CreateDateColumn()
  created_at: Date;
}
