import { BaseEntity } from '@edr/api-common';
import { Column, Entity, Index, JoinColumn, ManyToOne } from 'typeorm';

import { WarehouseYard } from './warehouse-yard.entity';

export const WAREHOUSE_ZONE_TYPES = [
  'CONTAINER_ZONE',
  'BULK_ZONE',
  'GENERAL_CARGO_ZONE',
  'HAZARDOUS_ZONE',
  'COLD_STORAGE_ZONE',
] as const;
export type WarehouseZoneType = (typeof WAREHOUSE_ZONE_TYPES)[number];

export const WAREHOUSE_ZONE_STATUSES = ['ACTIVE', 'INACTIVE'] as const;
export type WarehouseZoneStatus = (typeof WAREHOUSE_ZONE_STATUSES)[number];

@Entity({ schema: 'freight', name: 'warehouse_zones' })
@Index(['yardId'])
@Index(['type'])
@Index(['status'])
export class WarehouseZone extends BaseEntity {
  @Column({ name: 'yard_id', type: 'uuid' })
  yardId!: string;

  @ManyToOne(() => WarehouseYard, (yard) => yard.zones, { onDelete: 'CASCADE' })
  @JoinColumn({ name: 'yard_id' })
  yard?: WarehouseYard;

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

  @Column({ name: 'code', type: 'varchar', length: 40 })
  code!: string;

  @Column({ name: 'type', type: 'varchar', length: 32 })
  type!: WarehouseZoneType;

  @Column({ name: 'capacity_weight', type: 'numeric', precision: 14, scale: 3, nullable: true })
  capacityWeight?: number | null;

  @Column({ name: 'capacity_containers', type: 'int', nullable: true })
  capacityContainers?: number | null;

  @Column({ name: 'current_weight', type: 'numeric', precision: 14, scale: 3, default: 0 })
  currentWeight!: number;

  @Column({ name: 'current_containers', type: 'int', default: 0 })
  currentContainers!: number;

  @Column({ name: 'max_weight', type: 'numeric', precision: 14, scale: 3, nullable: true })
  maxWeight?: number | null;

  @Column({ name: 'max_volume', type: 'numeric', precision: 14, scale: 3, nullable: true })
  maxVolume?: number | null;

  @Column({ name: 'current_volume', type: 'numeric', precision: 14, scale: 3, default: 0 })
  currentVolume!: number;

  @Column({ name: 'status', type: 'varchar', length: 16, default: 'ACTIVE' })
  status!: WarehouseZoneStatus;

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