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

export const IMPORT_CUSTOMS_RISK_LEVELS = ['GREEN', 'YELLOW', 'BLUE', 'RED'] as const;
export type ImportCustomsRiskLevel = (typeof IMPORT_CUSTOMS_RISK_LEVELS)[number];

export type ImportCustomsDocumentType =
  | 'IM4'
  | 'IM5'
  | 'T1_CLOSURE_PROOF'
  | 'TRANSIT_PERMIT_SCREENSHOT'
  | 'CUSTOMER_PAYMENT_SLIP'
  | 'IMPORT_RELEASE_PERMIT';

@Entity({ schema: 'freight', name: 'import_customs_finalizations' })
@Index(['bookingId'], { unique: true })
@Index(['customsRisk'])
export class ImportCustomsFinalization extends BaseEntity {
  @Column({ name: 'booking_id', type: 'uuid' })
  bookingId!: string;

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

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

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

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

  @Column({ name: 'customs_risk', type: 'varchar', length: 12, nullable: true })
  customsRisk?: ImportCustomsRiskLevel | null;

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

  @Column({ name: 'completed_at', type: 'timestamptz', nullable: true })
  completedAt?: 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;
}
