import { Entity, PrimaryGeneratedColumn, Column, CreateDateColumn, UpdateDateColumn, Index, ManyToOne, JoinColumn } from 'typeorm';
import { InventoryCount } from './inventory-count.entity';

@Entity({ name: 'inventory_count_lines', schema: 'inventory' })
export class InventoryCountLine {
  @PrimaryGeneratedColumn('uuid')
  id: string;

  @Index()
  @Column({ name: 'count_id', type: 'uuid' })
  countId: string;

  @ManyToOne(() => InventoryCount, { onDelete: 'CASCADE' })
  @JoinColumn({ name: 'count_id' })
  count: InventoryCount;

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

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

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

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

  // Note: difference is GENERATED in DDL, but we calculate it in app layer
  @Column({ name: 'lot_number', type: 'varchar', length: 50, nullable: true })
  lotNumber?: string;

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

  @Index()
  @Column({ name: 'is_counted', type: 'boolean', default: false })
  isCounted: boolean;

  @Column({ name: 'counted_at', type: 'timestamptz', nullable: true })
  countedAt?: Date;

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

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

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

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