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

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

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

  // Tipo de movimiento
  @Index()
  @Column({ name: 'movement_type', type: 'varchar', length: 20 })
  movementType:
    | 'receipt'
    | 'shipment'
    | 'transfer'
    | 'adjustment'
    | 'return'
    | 'production'
    | 'consumption';

  @Index()
  @Column({ name: 'movement_number', type: 'varchar', length: 30 })
  movementNumber: string;

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

  // Origen y destino
  @Index()
  @Column({ name: 'source_warehouse_id', type: 'uuid', nullable: true })
  sourceWarehouseId: string;

  @Column({ name: 'source_location_id', type: 'uuid', nullable: true })
  sourceLocationId: string;

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

  @Column({ name: 'dest_location_id', type: 'uuid', nullable: true })
  destLocationId: string;

  // Cantidad
  @Column({ type: 'decimal', precision: 15, scale: 4 })
  quantity: number;

  @Column({ type: 'varchar', length: 20, default: 'PZA' })
  uom: string;

  // 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;

  @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;

  // Referencia
  @Index()
  @Column({ name: 'reference_type', type: 'varchar', length: 30, nullable: true })
  referenceType: string;

  @Column({ name: 'reference_id', type: 'uuid', nullable: true })
  referenceId: string;

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

  // Razon (para ajustes)
  @Column({ type: 'varchar', length: 100, nullable: true })
  reason: string;

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

  // Estado
  @Index()
  @Column({ type: 'varchar', length: 20, default: 'draft' })
  status: 'draft' | 'confirmed' | 'cancelled';

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

  @Column({ name: 'confirmed_by', type: 'uuid', nullable: true })
  confirmedBy: string;

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

  @Column({ name: 'created_by', type: 'uuid', nullable: true })
  createdBy: string;

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

  @DeleteDateColumn({ name: 'deleted_at', type: 'timestamptz', nullable: true })
  deletedAt: Date;
}
