import { BaseEntity } from '@edr/api-common';
import { Column, Entity, Index, JoinColumn, ManyToOne } from 'typeorm';
import { Booking } from '../../bookings/entities/booking.entity';

/**
 * Standard anomaly taxonomy for cargo exception reporting at a border/port
 * station (GL Import US-07 AC1.2). GL Djibouti logs one of these against a
 * shipment with a free-text narrative and photo evidence; a high-priority alert
 * is then surfaced to GL Ethiopia.
 */
export const INCIDENT_TYPES = [
  'SEAL_BROKEN',
  'CONTAINER_OPENED',
  'CONTAINER_DAMAGED',
  'FLUID_LEAKING',
  'OTHER',
] as const;
export type IncidentType = (typeof INCIDENT_TYPES)[number];

/**
 * A cargo exception/damage report raised during loading or handover. Attaches to
 * a booking (post-booking phase) and carries one or more photo file references
 * for verification. See docs/new-doc.md §14 gap #18, GL Import US-07.
 */
@Entity({ schema: 'freight', name: 'clearance_incidents' })
@Index(['bookingId'])
export class ClearanceIncident extends BaseEntity {
  @Column({ name: 'booking_id', type: 'uuid' })
  bookingId!: string;

  @ManyToOne(() => Booking, { onDelete: 'CASCADE' })
  @JoinColumn({ name: 'booking_id' })
  booking?: Booking;

  @Column({ name: 'incident_type', type: 'varchar', length: 32 })
  incidentType!: IncidentType;

  @Column({ name: 'description', type: 'text' })
  description!: string;

  /** MinIO file ids of the uploaded photo evidence (.jpg). */
  @Column({ name: 'photo_file_ids', type: 'jsonb', default: () => "'[]'" })
  photoFileIds!: string[];

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

  @Column({ name: 'reported_at', type: 'timestamptz', default: () => 'NOW()' })
  reportedAt!: Date;
}
