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

import { Yard } from './yard.entity';

/**
 * What a yard's load/unload facility can do. One record per yard flagged
 * `has_facility`.
 *
 * `hasWarehouse` is the line that matters: a facility with a warehouse (Indode
 * today) stores cargo and therefore accrues storage/demurrage through the normal
 * warehouse flow; the rest only move cargo on and off the train, so they record
 * the handling event and its GRN and nothing else.
 */
@Entity({ schema: 'freight', name: 'yard_facilities' })
@Index(['yardId'])
export class YardFacility extends BaseEntity {
  @Column({ name: 'yard_id', type: 'uuid' })
  yardId!: string;

  @OneToOne(() => Yard, { nullable: false, onDelete: 'CASCADE' })
  @JoinColumn({ name: 'yard_id' })
  yard?: Yard;

  /** Cargo can be stored here — enables the warehouse flow (storage, demurrage). */
  @Column({ name: 'has_warehouse', type: 'boolean', default: false })
  hasWarehouse!: boolean;

  /**
   * Containers need a reach stacker or gantry, so only the equipped facilities
   * (Indode, Modjo, Dire Dawa) take them. Bulk needs far less and is handled
   * everywhere.
   */
  @Column({ name: 'handles_container', type: 'boolean', default: true })
  handlesContainer!: boolean;

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

  @Column({ name: 'equipment_notes', type: 'text', nullable: true })
  equipmentNotes?: string | null;

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