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

import { InterchangeDocumentItem } from './interchange-document-item.entity';

export const INTERCHANGE_DIRECTIONS = ['IMPORT', 'EXPORT'] as const;
export type InterchangeDirection = (typeof INTERCHANGE_DIRECTIONS)[number];

export const INTERCHANGE_DOCUMENT_STATUSES = [
  'DRAFT',
  'GENERATED',
  'ACKNOWLEDGED',
  'DISPUTED',
  'CANCELLED',
] as const;
export type InterchangeDocumentStatus = (typeof INTERCHANGE_DOCUMENT_STATUSES)[number];

@Entity({ schema: 'freight', name: 'interchange_documents' })
@Index(['documentNo'], { unique: true })
@Index(['direction'])
@Index(['status'])
@Index(['scheduleId'])
export class InterchangeDocument extends BaseEntity {
  @Column({ name: 'document_no', type: 'varchar', length: 40, unique: true })
  documentNo!: string;

  @Column({ name: 'direction', type: 'varchar', length: 10 })
  direction!: InterchangeDirection;

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

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

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

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

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

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

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

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

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

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

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

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

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

  @Column({ name: 'status', type: 'varchar', length: 20, default: 'DRAFT' })
  status!: InterchangeDocumentStatus;

  @Column({ name: 'generated_at', type: 'timestamptz', nullable: true })
  generatedAt?: Date | null;

  @Column({ name: 'acknowledged_at', type: 'timestamptz', nullable: true })
  acknowledgedAt?: Date | null;

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

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

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

  @OneToMany(() => InterchangeDocumentItem, (item) => item.document)
  items?: InterchangeDocumentItem[];
}
