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

export class CreateBookingOrders1792000000002 implements MigrationInterface {
  name = 'CreateBookingOrders1792000000002';

  public async up(queryRunner: QueryRunner): Promise<void> {
    await queryRunner.createTable(
      new Table({
        schema: 'freight',
        name: 'booking_orders',
        columns: [
          { name: 'id', type: 'uuid', isPrimary: true, generationStrategy: 'uuid', default: 'gen_random_uuid()' },
          { name: 'reference', type: 'varchar', length: '64', isUnique: true },
          { name: 'contract_booking_id', type: 'uuid' },
          { name: 'booking_id', type: 'uuid', isNullable: true },
          { name: 'company_id', type: 'uuid', isNullable: true },
          { name: 'scheduled_date', type: 'timestamptz' },
          { name: 'status', type: 'varchar', length: '40', default: "'PAID'" },
          { name: 'scheduling_status', type: 'varchar', length: '30', default: "'NOT_SCHEDULED'" },
          { name: 'train_schedule_id', type: 'uuid', isNullable: true },
          { name: 'created_at', type: 'timestamptz', default: 'now()' },
          { name: 'updated_at', type: 'timestamptz', default: 'now()' },
          { name: 'deleted_at', type: 'timestamptz', isNullable: true },
        ],
      }),
      true,
    );

    await queryRunner.createIndex(
      'freight.booking_orders',
      new TableIndex({ name: 'idx_booking_orders_contract', columnNames: ['contract_booking_id'] }),
    );
    await queryRunner.createIndex(
      'freight.booking_orders',
      new TableIndex({ name: 'idx_booking_orders_company', columnNames: ['company_id'] }),
    );

    await queryRunner.createTable(
      new Table({
        schema: 'freight',
        name: 'booking_order_lines',
        columns: [
          { name: 'id', type: 'uuid', isPrimary: true, generationStrategy: 'uuid', default: 'gen_random_uuid()' },
          { name: 'order_id', type: 'uuid' },
          { name: 'container_type_id', type: 'uuid', isNullable: true },
          { name: 'quantity', type: 'numeric', precision: 12, scale: 3 },
          { name: 'created_at', type: 'timestamptz', default: 'now()' },
          { name: 'updated_at', type: 'timestamptz', default: 'now()' },
          { name: 'deleted_at', type: 'timestamptz', isNullable: true },
        ],
        foreignKeys: [
          {
            columnNames: ['order_id'],
            referencedSchema: 'freight',
            referencedTableName: 'booking_orders',
            referencedColumnNames: ['id'],
            onDelete: 'CASCADE',
          },
        ],
      }),
      true,
    );

    await queryRunner.createIndex(
      'freight.booking_order_lines',
      new TableIndex({ name: 'idx_booking_order_lines_order', columnNames: ['order_id'] }),
    );
  }

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