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

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

  async up(queryRunner: QueryRunner): Promise<void> {
    await queryRunner.createTable(
      new Table({
        schema: 'freight',
        name: 'djibouti_import_incidents',
        columns: [
          { name: 'id', type: 'uuid', isPrimary: true, generationStrategy: 'uuid', default: 'uuid_generate_v4()' },
          { name: 'booking_id', type: 'uuid' },
          { name: 'container_number', type: 'varchar', length: '80', isNullable: true },
          { name: 'cargo_id', type: 'uuid', isNullable: true },
          { name: 'facility', type: 'varchar', length: '120', isNullable: true },
          { name: 'station', type: 'varchar', length: '120', isNullable: true },
          { name: 'incident_type', type: 'varchar', length: '40' },
          { name: 'description', type: 'text' },
          { name: 'photos', type: 'jsonb', default: "'[]'::jsonb" },
          { name: 'reported_by', type: 'varchar', length: '120', isNullable: true },
          { name: 'reported_at', type: 'timestamptz' },
          { 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.djibouti_import_incidents', new TableIndex({ name: 'idx_djibouti_incidents_booking', columnNames: ['booking_id'] }));
    await queryRunner.createIndex('freight.djibouti_import_incidents', new TableIndex({ name: 'idx_djibouti_incidents_container', columnNames: ['container_number'] }));
    await queryRunner.createIndex('freight.djibouti_import_incidents', new TableIndex({ name: 'idx_djibouti_incidents_type', columnNames: ['incident_type'] }));

    await queryRunner.createTable(
      new Table({
        schema: 'freight',
        name: 'import_customs_finalizations',
        columns: [
          { name: 'id', type: 'uuid', isPrimary: true, generationStrategy: 'uuid', default: 'uuid_generate_v4()' },
          { name: 'booking_id', type: 'uuid', isUnique: true },
          { name: 'documents', type: 'jsonb', default: "'{}'::jsonb" },
          { name: 'declaration_serial_number', type: 'varchar', length: '120', isNullable: true },
          { name: 'duties_taxes_notified_at', type: 'timestamptz', isNullable: true },
          { name: 'duties_taxes_paid_at', type: 'timestamptz', isNullable: true },
          { name: 'customs_risk', type: 'varchar', length: '12', isNullable: true },
          { name: 'import_release_permitted_at', type: 'timestamptz', isNullable: true },
          { name: 'completed_at', type: 'timestamptz', isNullable: true },
          { name: 'performed_by', type: 'varchar', length: '120', isNullable: true },
          { name: 'notes', 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 },
        ],
      }),
      true,
    );
    await queryRunner.createIndex('freight.import_customs_finalizations', new TableIndex({ name: 'idx_import_customs_booking', columnNames: ['booking_id'] }));
    await queryRunner.createIndex('freight.import_customs_finalizations', new TableIndex({ name: 'idx_import_customs_risk', columnNames: ['customs_risk'] }));

    await queryRunner.createTable(
      new Table({
        schema: 'freight',
        name: 'empty_container_returns',
        columns: [
          { name: 'id', type: 'uuid', isPrimary: true, generationStrategy: 'uuid', default: 'uuid_generate_v4()' },
          { name: 'container_number', type: 'varchar', length: '80' },
          { name: 'booking_id', type: 'uuid', isNullable: true },
          { name: 'customer_id', type: 'uuid', isNullable: true },
          { name: 'return_date', type: 'timestamptz' },
          { name: 'facility', type: 'varchar', length: '120', isNullable: true },
          { name: 'yard', type: 'varchar', length: '120', isNullable: true },
          { name: 'zone', type: 'varchar', length: '120', isNullable: true },
          { name: 'condition', type: 'text', isNullable: true },
          { name: 'handover_note', type: 'text', isNullable: true },
          { name: 'status', type: 'varchar', length: '40', default: "'RETURNED'" },
          { name: 'wagon_allocation_reference', type: 'varchar', length: '120', isNullable: true },
          { name: 'performed_by', type: 'varchar', length: '120', 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.empty_container_returns', new TableIndex({ name: 'idx_empty_returns_container', columnNames: ['container_number'] }));
    await queryRunner.createIndex('freight.empty_container_returns', new TableIndex({ name: 'idx_empty_returns_booking', columnNames: ['booking_id'] }));
    await queryRunner.createIndex('freight.empty_container_returns', new TableIndex({ name: 'idx_empty_returns_status', columnNames: ['status'] }));
  }

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