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

/**
 * Customer shipment requests for GENERAL customs (Path B) contracts. The customer
 * submits date + quantities; Global Logistics reviews, then creates the booking
 * on their behalf and per-booking clearance begins. Additive — no change to
 * existing tables; ONE_TIME contracts are unaffected.
 */
export class CreateBookingRequests1827000000000 implements MigrationInterface {
  name = 'CreateBookingRequests1827000000000';

  public async up(queryRunner: QueryRunner): Promise<void> {
    await queryRunner.createTable(
      new Table({
        schema: 'freight',
        name: 'booking_requests',
        columns: [
          { name: 'id', type: 'uuid', isPrimary: true, generationStrategy: 'uuid', default: 'gen_random_uuid()' },
          { name: 'reference', type: 'varchar', length: '40', default: "''" },
          { name: 'contract_id', type: 'uuid' },
          { name: 'requested_by_user_id', type: 'uuid', isNullable: true },
          { name: 'contract_route_id', type: 'uuid', isNullable: true },
          { name: 'scheduled_date', type: 'timestamptz', isNullable: true },
          { name: 'status', type: 'varchar', length: '16', default: "'PENDING'" },
          { name: 'requested_lines', type: 'jsonb', default: "'{}'::jsonb" },
          { name: 'notes', type: 'text', isNullable: true },
          { name: 'created_booking_id', type: 'uuid', isNullable: true },
          { name: 'reviewed_by_staff_id', type: 'uuid', isNullable: true },
          { name: 'reviewed_at', type: 'timestamptz', isNullable: true },
          { name: 'review_note', type: 'text', isNullable: true },
          { name: 'created_at', type: 'timestamptz', default: 'now()' },
          { name: 'updated_at', type: 'timestamptz', default: 'now()' },
          { name: 'deleted_at', type: 'timestamptz', isNullable: true },
        ],
        foreignKeys: [
          {
            columnNames: ['contract_id'],
            referencedSchema: 'freight',
            referencedTableName: 'contracts',
            referencedColumnNames: ['id'],
            onDelete: 'CASCADE',
          },
          {
            columnNames: ['created_booking_id'],
            referencedSchema: 'freight',
            referencedTableName: 'bookings',
            referencedColumnNames: ['id'],
            onDelete: 'SET NULL',
          },
        ],
      }),
      true,
    );

    await queryRunner.createIndex(
      'freight.booking_requests',
      new TableIndex({ name: 'idx_booking_requests_contract', columnNames: ['contract_id'] }),
    );
    await queryRunner.createIndex(
      'freight.booking_requests',
      new TableIndex({ name: 'idx_booking_requests_status', columnNames: ['status'] }),
    );
    await queryRunner.createIndex(
      'freight.booking_requests',
      new TableIndex({
        name: 'idx_booking_requests_contract_status',
        columnNames: ['contract_id', 'status'],
      }),
    );
  }

  public async down(queryRunner: QueryRunner): Promise<void> {
    await queryRunner.dropTable('freight.booking_requests', true);
  }
}
