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

import { WarehouseInventory } from './warehouse-inventory.entity';

export const INSPECTION_REPORT_TYPES = [
  'INSPECTION',
  'DAMAGE',
  'WEIGHT_LOSS',
  'MISSING_ITEM',
  'GENERAL',
] as const;
export type InspectionReportType = (typeof INSPECTION_REPORT_TYPES)[number];

export const INSPECTION_STATUSES = ['PASSED', 'FAILED', 'NEEDS_REVIEW'] as const;
export type InspectionStatus = (typeof INSPECTION_STATUSES)[number];

@Entity({ schema: 'freight', name: 'warehouse_inspection_reports' })
@Index(['inventoryId'])
@Index(['bookingId'])
@Index(['inspectionStatus'])
export class WarehouseInspectionReport extends BaseEntity {
  @Column({ name: 'inventory_id', type: 'uuid' })
  inventoryId!: string;

  @ManyToOne(() => WarehouseInventory)
  @JoinColumn({ name: 'inventory_id' })
  inventory?: WarehouseInventory;

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

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

  @Column({ name: 'report_type', type: 'varchar', length: 32, default: 'INSPECTION' })
  reportType!: InspectionReportType;

  @Column({ name: 'inspection_status', type: 'varchar', length: 20, default: 'NEEDS_REVIEW' })
  inspectionStatus!: InspectionStatus;

  @Column({ name: 'has_damage', type: 'boolean', default: false })
  hasDamage!: boolean;

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

  @Column({ name: 'has_weight_loss', type: 'boolean', default: false })
  hasWeightLoss!: boolean;

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

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

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

  @Column({ name: 'weight_loss_unit', type: 'varchar', length: 12, nullable: true })
  weightLossUnit?: string | null;

  @Column({ name: 'has_missing_items', type: 'boolean', default: false })
  hasMissingItems!: boolean;

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

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

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

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