import { Entity, PrimaryGeneratedColumn, Column, CreateDateColumn, UpdateDateColumn, Index, ManyToOne, JoinColumn } from 'typeorm';
import { PurchaseReceipt } from './purchase-receipt.entity';

@Entity({ name: 'purchase_receipt_items', schema: 'purchases' })
export class PurchaseReceiptItem {
  @PrimaryGeneratedColumn('uuid')
  id: string;

  @Index()
  @Column({ name: 'receipt_id', type: 'uuid' })
  receiptId: string;

  @ManyToOne(() => PurchaseReceipt, { onDelete: 'CASCADE' })
  @JoinColumn({ name: 'receipt_id' })
  receipt: PurchaseReceipt;

  @Column({ name: 'order_item_id', type: 'uuid', nullable: true })
  orderItemId?: string;

  @Index()
  @Column({ name: 'product_id', type: 'uuid', nullable: true })
  productId?: string;

  @Column({ name: 'quantity_expected', type: 'decimal', precision: 15, scale: 4, nullable: true })
  quantityExpected?: number;

  @Column({ name: 'quantity_received', type: 'decimal', precision: 15, scale: 4 })
  quantityReceived: number;

  @Column({ name: 'quantity_rejected', type: 'decimal', precision: 15, scale: 4, default: 0 })
  quantityRejected: number;

  @Index()
  @Column({ name: 'lot_number', type: 'varchar', length: 50, nullable: true })
  lotNumber?: string;

  @Column({ name: 'serial_number', type: 'varchar', length: 50, nullable: true })
  serialNumber?: string;

  @Column({ name: 'expiry_date', type: 'date', nullable: true })
  expiryDate?: Date;

  @Column({ name: 'location_id', type: 'uuid', nullable: true })
  locationId?: string;

  @Column({ name: 'quality_status', type: 'varchar', length: 20, default: 'pending' })
  qualityStatus: 'pending' | 'approved' | 'rejected' | 'quarantine';

  @Column({ name: 'quality_notes', type: 'text', nullable: true })
  qualityNotes?: string;

  @CreateDateColumn({ name: 'created_at', type: 'timestamptz' })
  createdAt: Date;

  @UpdateDateColumn({ name: 'updated_at', type: 'timestamptz' })
  updatedAt: Date;
}
