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

import { Booking } from '../../bookings/entities/booking.entity';
import { InterchangeDocument } from './interchange-document.entity';

export const INTERCHANGE_ITEM_TYPES = ['CONTAINER', 'CARGO'] as const;
export type InterchangeItemType = (typeof INTERCHANGE_ITEM_TYPES)[number];

export const INTERCHANGE_CONDITION_STATUSES = [
  'GOOD',
  'DAMAGED',
  'SHORTAGE',
  'EXCESS',
  'HOLD',
  'UNKNOWN',
] as const;
export type InterchangeConditionStatus = (typeof INTERCHANGE_CONDITION_STATUSES)[number];

@Entity({ schema: 'freight', name: 'interchange_document_items' })
@Index(['interchangeDocumentId'])
@Index(['bookingId'])
export class InterchangeDocumentItem extends BaseEntity {
  @Column({ name: 'interchange_document_id', type: 'uuid' })
  interchangeDocumentId!: string;

  @ManyToOne(() => InterchangeDocument, (document) => document.items, { onDelete: 'CASCADE' })
  @JoinColumn({ name: 'interchange_document_id' })
  document?: InterchangeDocument;

  @Column({ name: 'booking_id', type: 'uuid', nullable: true })
  bookingId?: string | null;

  @ManyToOne(() => Booking, { nullable: true, onDelete: 'SET NULL' })
  @JoinColumn({ name: 'booking_id' })
  booking?: Booking | null;

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

  @Column({ name: 'item_type', type: 'varchar', length: 20 })
  itemType!: InterchangeItemType;

  @Column({ name: 'booking_container_id', type: 'uuid', nullable: true })
  bookingContainerId?: string | null;

  @Column({ name: 'booking_cargo_id', type: 'uuid', nullable: true })
  bookingCargoId?: string | null;

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

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

  @Column({ name: 'cargo_id', type: 'uuid', nullable: true })
  cargoId?: string | null;

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

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

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

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

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

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

  @Column({ name: 'condition_status', type: 'varchar', length: 20, default: 'GOOD' })
  conditionStatus!: InterchangeConditionStatus;

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

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