import {
  Entity,
  Column,
  PrimaryGeneratedColumn,
  ManyToOne,
  CreateDateColumn,
  UpdateDateColumn,
  JoinColumn,
} from 'typeorm';
import { Company } from '../../companies/entities/company.entity';
import { Standard } from '../../standards/entities/standard.entity';

@Entity('new_certificate')
export class NewCertificate {
  @PrimaryGeneratedColumn()
  id: number;

  /** RELATIONSHIPS **/
  @ManyToOne(() => Company)
  @JoinColumn({ name: 'company_id' })
  company: Company;

  @ManyToOne(() => Standard)
  @JoinColumn({ name: 'standard_id' })
  standard: Standard;

  // ✅ NEW — link to previous cert (for surveillance/recertification)
  @ManyToOne(() => NewCertificate, { nullable: true })
  @JoinColumn({ name: 'previous_certificate_id' })
  previous_certificate: NewCertificate;

  /** SNAPSHOT FIELDS **/
  @Column({ type: 'varchar', length: 255 })
  company_name_snapshot: string;

  @Column({ type: 'varchar', length: 500 })
  address_snapshot: string;

  @Column({ type: 'varchar', length: 100 })
  city: string;

  @Column({ type: 'varchar', length: 100 })
  country: string;

  @Column({ type: 'varchar', length: 255 })
  standard_name: string;

  @Column({ type: 'varchar', length: 10 })
  standard_short: string;

  /** CERTIFICATE DETAILS **/
  @Column('text')
  scope_of_work: string;

  @Column({ type: 'varchar', length: 30, unique: true })
  certificate_no: string;

  @Column({ type: 'varchar', length: 50 })
  ea_codes: string;
  
  @Column({ type: 'date', nullable: true })
  surveillance_audit_due: Date | null;

  @Column({ type: 'date', nullable: true })
  recertification_due: Date | null;
  // ✅ NEW — cert type
  @Column({
    type: 'enum',
    enum: ['INITIAL', 'SURVEILLANCE', 'RECERTIFICATION'],
    default: 'INITIAL',
  })
  cert_type: string;

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

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

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

  @Column({ type: 'varchar', length: 36, unique: true })
  qrcode_token: string;

  @Column({ type: 'varchar', length: 50, nullable: true })
  fingerprint: string;

  @Column({
    type: 'enum',
    enum: ['local', 'international'],
    default: 'local',
  })
  verification_domain: string;

  // ❌ REMOVED — pdf_url (no more template-generated PDF)
  // @Column({ type: 'varchar', nullable: true })
  // pdf_url: string;

  // ✅ NEW — the uploaded signed scan (the only PDF)
  @Column({ type: 'varchar', length: 500, nullable: true })
  scan_pdf_url: string;

  // ✅ NEW — when scan was uploaded
  @Column({ type: 'timestamp', nullable: true })
  scan_uploaded_at: Date;

  // ✅ NEW — who uploaded the scan
  @Column({ type: 'int', nullable: true })
  scan_uploaded_by: number;

  // ✅ UPDATED — status enum (added pending_scan + superseded)
  @Column({
    type: 'enum',
    enum: [
      'pending_scan',
      'active',
      'superseded',
      'expired',
      'revoked',
      'fake',
    ],
    default: 'pending_scan',
  })
  status: string;

  // ✅ NEW — who created
  @Column({ type: 'int', nullable: true })
  created_by: number;

  // ✅ NEW — who updated
  @Column({ type: 'int', nullable: true })
  updated_by: number;

  @CreateDateColumn()
  created_at: Date;

  @UpdateDateColumn()
  updated_at: Date;
}
