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

import { Warehouse } from './warehouse.entity';
import { WarehouseZone } from './warehouse-zone.entity';

export const WAREHOUSE_YARD_TYPES = [
  'CONTAINER_YARD',
  'BULK_YARD',
  'GENERAL_CARGO_YARD',
  'HAZARDOUS_YARD',
  'COLD_STORAGE_YARD',
] as const;
export type WarehouseYardType = (typeof WAREHOUSE_YARD_TYPES)[number];

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

@Entity({ schema: 'freight', name: 'warehouse_yards' })
@Index(['warehouseId'])
@Index(['type'])
@Index(['status'])
export class WarehouseYard extends BaseEntity {
  @Column({ name: 'warehouse_id', type: 'uuid' })
  warehouseId!: string;

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

  @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!: WarehouseYardType;

  @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!: WarehouseYardStatus;

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

  @OneToMany(() => WarehouseZone, (zone) => zone.yard)
  zones?: WarehouseZone[];
}
