import {
  Entity,
  PrimaryGeneratedColumn,
  Column,
  CreateDateColumn,
  UpdateDateColumn,
  Index,
} from 'typeorm';

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

  @Index()
  @Column({ name: 'tenant_id', type: 'uuid' })
  tenantId: string;

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

  @Index()
  @Column({ name: 'warehouse_id', type: 'uuid' })
  warehouseId: string;

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

  // Cantidades
  @Column({ name: 'quantity_on_hand', type: 'decimal', precision: 15, scale: 4, default: 0 })
  quantityOnHand: number;

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

  // quantity_available es calculado en DDL como GENERATED COLUMN, lo leemos aquí
  @Column({
    name: 'quantity_available',
    type: 'decimal',
    precision: 15,
    scale: 4,
    insert: false,
    update: false,
  })
  quantityAvailable: number;

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

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

  // Lote y serie
  @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;

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

  // Costo
  @Column({ name: 'unit_cost', type: 'decimal', precision: 15, scale: 4, nullable: true })
  unitCost: number;

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

  // Ultima actividad
  @Column({ name: 'last_movement_at', type: 'timestamptz', nullable: true })
  lastMovementAt: Date;

  @Column({ name: 'last_count_at', type: 'timestamptz', nullable: true })
  lastCountAt: Date;

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

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