import { MigrationInterface, QueryRunner, Table, TableColumn } from 'typeorm';

/**
 * Batch 4.5 — warehouse inspection reports + inventory inspection status.
 */
export class AddWarehouseInspection1750000000003 implements MigrationInterface {
  public async up(queryRunner: QueryRunner): Promise<void> {
    // inventory.inspection_status
    const inventoryTable = await queryRunner.getTable('freight.warehouse_inventory');
    if (inventoryTable) {
      const hasColumn = inventoryTable.columns.some((col) => col.name === 'inspection_status');
      if (!hasColumn) {
        await queryRunner.addColumn(
          'freight.warehouse_inventory',
          new TableColumn({ name: 'inspection_status', type: 'varchar', length: '20', isNullable: true }),
        );
      }
    }

    // warehouse_inspection_reports table
    const inspectionTable = await queryRunner.getTable('freight.warehouse_inspection_reports');
    if (!inspectionTable) {
      await queryRunner.createTable(
        new Table({
          schema: 'freight',
          name: 'warehouse_inspection_reports',
          columns: [
            { name: 'id', type: 'uuid', isPrimary: true, default: 'uuid_generate_v4()' },
            { name: 'inventory_id', type: 'uuid' },
            { name: 'booking_id', type: 'uuid', isNullable: true },
            { name: 'customer_id', type: 'uuid', isNullable: true },
            { name: 'report_type', type: 'varchar', length: '32', default: "'INSPECTION'" },
            { name: 'inspection_status', type: 'varchar', length: '20', default: "'NEEDS_REVIEW'" },
            { name: 'has_damage', type: 'boolean', default: false },
            { name: 'damage_description', type: 'text', isNullable: true },
            { name: 'has_weight_loss', type: 'boolean', default: false },
            { name: 'expected_weight', type: 'numeric', precision: 14, scale: 3, isNullable: true },
            { name: 'actual_weight', type: 'numeric', precision: 14, scale: 3, isNullable: true },
            { name: 'weight_loss', type: 'numeric', precision: 14, scale: 3, isNullable: true },
            { name: 'weight_loss_unit', type: 'varchar', length: '12', isNullable: true },
            { name: 'has_missing_items', type: 'boolean', default: false },
            { name: 'missing_items_description', type: 'text', isNullable: true },
            { name: 'remarks', type: 'text', isNullable: true },
            { name: 'inspected_by_id', type: 'uuid', isNullable: true },
            { name: 'inspected_at', type: 'timestamptz', isNullable: true },
            { name: 'created_at', type: 'timestamptz', default: 'now()' },
            { name: 'updated_at', type: 'timestamptz', default: 'now()' },
            { name: 'deleted_at', type: 'timestamptz', isNullable: true },
          ],
          indices: [
            { name: 'idx_wir_inventory', columnNames: ['inventory_id'] },
            { name: 'idx_wir_booking', columnNames: ['booking_id'] },
            { name: 'idx_wir_status', columnNames: ['inspection_status'] },
          ],
        }),
        true,
      );
    }
  }

  public async down(queryRunner: QueryRunner): Promise<void> {
    const inspectionTable = await queryRunner.getTable('freight.warehouse_inspection_reports');
    if (inspectionTable) {
      await queryRunner.dropTable('freight.warehouse_inspection_reports', true);
    }

    const inventoryTable = await queryRunner.getTable('freight.warehouse_inventory');
    if (inventoryTable) {
      const hasColumn = inventoryTable.columns.some((col) => col.name === 'inspection_status');
      if (hasColumn) {
        await queryRunner.dropColumn('freight.warehouse_inventory', 'inspection_status');
      }
    }
  }
}
