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

import { Warehouse } from '../../warehouses/entities/warehouse.entity';

export const FACILITY_TYPES = ['PORT', 'DRY_PORT', 'TERMINAL', 'RAIL_YARD', 'WAREHOUSE_COMPLEX'] as const;
export type FacilityType = (typeof FACILITY_TYPES)[number];

export const FACILITY_STATUSES = ['ACTIVE', 'INACTIVE', 'UNDER_MAINTENANCE'] as const;
export type FacilityStatus = (typeof FACILITY_STATUSES)[number];

@Entity({ schema: 'freight', name: 'facilities' })
@Index(['code'], { unique: true })
@Index(['facilityStatus'])
export class Facility extends BaseEntity {
  @Column({ name: 'code', type: 'varchar', length: 40, unique: true })
  code!: string;

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

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

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

  @Column({ name: 'facility_status', type: 'varchar', length: 32, default: 'ACTIVE' })
  facilityStatus!: FacilityStatus;

  @Column({ name: 'location_name', type: 'varchar', length: 200, nullable: true })
  locationName?: string | null;

  @Column({ name: 'country', type: 'varchar', length: 100, nullable: true })
  country?: string | null;

  @Column({ name: 'city', type: 'varchar', length: 100, nullable: true })
  city?: string | null;

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

  @Column({ name: 'latitude', type: 'numeric', precision: 10, scale: 8, nullable: true })
  latitude?: number | null;

  @Column({ name: 'longitude', type: 'numeric', precision: 11, scale: 8, nullable: true })
  longitude?: number | null;

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

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

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

  @OneToMany(() => Warehouse, (warehouse) => warehouse.facility)
  warehouses?: Warehouse[];
}
