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

import { TrainSchedule } from '../../train-schedules/entities/train-schedule.entity';

export type ImportDjiboutiDocumentType =
  | 'GATE_PASS'
  | 'DELIVERY_ORDER'
  | 'PORT_INVOICE'
  | 'DJIBOUTI_T1'
  | 'ETHIOPIA_T1'
  | 'RAILWAY_BILL';

export interface ImportDjiboutiDocumentRecord {
  fileId?: string | null;
  fileUrl?: string | null;
  reference?: string | null;
  uploadedAt: string;
  uploadedBy?: string | null;
  notes?: string | null;
}

@Entity({ schema: 'freight', name: 'import_djibouti_operations' })
@Index(['trainScheduleId'], { unique: true })
export class ImportDjiboutiOperation extends BaseEntity {
  @Column({ name: 'train_schedule_id', type: 'uuid' })
  trainScheduleId!: string;

  @OneToOne(() => TrainSchedule)
  @JoinColumn({ name: 'train_schedule_id' })
  trainSchedule?: TrainSchedule;

  @Column({ name: 'documents', type: 'jsonb', default: () => "'{}'::jsonb" })
  documents!: Partial<Record<ImportDjiboutiDocumentType, ImportDjiboutiDocumentRecord>>;

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

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

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

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

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

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

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