import {
  Entity,
  PrimaryGeneratedColumn,
  Column,
  CreateDateColumn,
  UpdateDateColumn,
  ManyToOne,
  JoinColumn,
  Index,
} from 'typeorm';
import { NcEntity } from './nc.entity';

/**
 * ncr_entries in scheme_dbs s — one finding belonging to a NEW NC.
 */
@Entity({ name: 'ncr_entries' })
export class NcrEntryEntity {
  @PrimaryGeneratedColumn({ type: 'bigint', unsigned: true })
  id: number;

  @Index()
  @Column({ type: 'bigint', unsigned: true })
  nc_id: number;

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

  @Column({ type: 'text', nullable: true })
  ncr_statement: string | null;

  @Column({ type: 'varchar', length: 255, nullable: true })
  criteria_clause: string | null;

  @Column({ type: 'text', nullable: true })
  corrective_action: string | null;

  @Column({ type: 'varchar', length: 512, nullable: true })
  document_path: string | null; // evidence file (reuses Phase 1 storage later)

  @Column({ type: 'varchar', length: 20, default: 'open' })
  status: string; // open / pending / closed

  @CreateDateColumn({ type: 'datetime' })
  created_at: Date;

  @UpdateDateColumn({ type: 'datetime' })
  updated_at: Date;

  @ManyToOne(() => NcEntity, (nc) => nc.entries, { onDelete: 'CASCADE' })
  @JoinColumn({ name: 'nc_id' })
  nc: NcEntity;
}
