import {
  Entity,
  PrimaryGeneratedColumn,
  Column,
  CreateDateColumn,
  UpdateDateColumn,
  DeleteDateColumn,
  Index,
  ManyToOne,
  JoinColumn,
} from 'typeorm';
import { Warehouse } from './warehouse.entity';

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

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

  @ManyToOne(() => Warehouse, { onDelete: 'CASCADE' })
  @JoinColumn({ name: 'warehouse_id' })
  warehouse: Warehouse;

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

  @ManyToOne(() => WarehouseLocation, { nullable: true, onDelete: 'SET NULL' })
  @JoinColumn({ name: 'parent_id' })
  parent: WarehouseLocation;

  // Identificacion
  @Index()
  @Column({ type: 'varchar', length: 30 })
  code: string;

  @Column({ type: 'varchar', length: 100 })
  name: string;

  @Index()
  @Column({ type: 'varchar', length: 50, nullable: true })
  barcode: string;

  // Tipo de ubicacion
  @Index()
  @Column({ name: 'location_type', type: 'varchar', length: 20, default: 'shelf' })
  locationType: 'zone' | 'aisle' | 'rack' | 'shelf' | 'bin';

  // Jerarquia
  @Column({ name: 'hierarchy_path', type: 'text', nullable: true })
  hierarchyPath: string;

  @Column({ name: 'hierarchy_level', type: 'int', default: 0 })
  hierarchyLevel: number;

  // Coordenadas dentro del almacen
  @Column({ type: 'varchar', length: 10, nullable: true })
  aisle: string;

  @Column({ type: 'varchar', length: 10, nullable: true })
  rack: string;

  @Column({ type: 'varchar', length: 10, nullable: true })
  shelf: string;

  @Column({ type: 'varchar', length: 10, nullable: true })
  bin: string;

  // Capacidad
  @Column({ name: 'capacity_units', type: 'int', nullable: true })
  capacityUnits: number;

  @Column({ name: 'capacity_volume', type: 'decimal', precision: 10, scale: 4, nullable: true })
  capacityVolume: number;

  @Column({ name: 'capacity_weight', type: 'decimal', precision: 10, scale: 4, nullable: true })
  capacityWeight: number;

  // Restricciones
  @Column({ name: 'allowed_product_types', type: 'text', array: true, default: '{}' })
  allowedProductTypes: string[];

  @Column({ name: 'temperature_range', type: 'jsonb', nullable: true })
  temperatureRange: { min?: number; max?: number };

  @Column({ name: 'humidity_range', type: 'jsonb', nullable: true })
  humidityRange: { min?: number; max?: number };

  // Estado
  @Column({ name: 'is_active', type: 'boolean', default: true })
  isActive: boolean;

  @Column({ name: 'is_pickable', type: 'boolean', default: true })
  isPickable: boolean;

  @Column({ name: 'is_receivable', type: 'boolean', default: true })
  isReceivable: boolean;

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

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

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