import { MigrationInterface, QueryRunner } from 'typeorm';

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

  public async up(queryRunner: QueryRunner): Promise<void> {
    await queryRunner.query(`
      ALTER TABLE freight.wagons
      ADD COLUMN IF NOT EXISTS "current_yard_id" UUID NULL;
    `);
    await queryRunner.query(`
      DO $$
      BEGIN
        IF NOT EXISTS (
          SELECT 1 FROM pg_constraint WHERE conname = 'FK_wagon_current_yard'
        ) THEN
          ALTER TABLE freight.wagons
          ADD CONSTRAINT "FK_wagon_current_yard"
          FOREIGN KEY ("current_yard_id") REFERENCES freight.yards(id) ON DELETE SET NULL;
        END IF;
      END $$;
    `);
    await queryRunner.query(`
      CREATE INDEX IF NOT EXISTS "IDX_wagon_current_yard_id"
      ON freight.wagons ("current_yard_id");
    `);

    await queryRunner.query(`DROP INDEX IF EXISTS freight.idx_wagons_readiness`);
    await queryRunner.query(`ALTER TABLE freight.wagons DROP COLUMN IF EXISTS readiness;`);

    await queryRunner.query(`
      ALTER TABLE freight.locomotives
      ADD COLUMN IF NOT EXISTS "current_yard_id" UUID NULL;
    `);
    await queryRunner.query(`
      DO $$
      BEGIN
        IF NOT EXISTS (
          SELECT 1 FROM pg_constraint WHERE conname = 'FK_locomotive_current_yard'
        ) THEN
          ALTER TABLE freight.locomotives
          ADD CONSTRAINT "FK_locomotive_current_yard"
          FOREIGN KEY ("current_yard_id") REFERENCES freight.yards(id) ON DELETE SET NULL;
        END IF;
      END $$;
    `);
    await queryRunner.query(`
      CREATE INDEX IF NOT EXISTS "IDX_locomotive_current_yard_id"
      ON freight.locomotives ("current_yard_id");
    `);

    await queryRunner.query(`DROP INDEX IF EXISTS freight.idx_locomotives_readiness`);
    await queryRunner.query(`ALTER TABLE freight.locomotives DROP COLUMN IF EXISTS readiness;`);
  }

  public async down(queryRunner: QueryRunner): Promise<void> {
    await queryRunner.query(`
      ALTER TABLE freight.wagons
      ADD COLUMN IF NOT EXISTS readiness VARCHAR(20) NOT NULL DEFAULT 'IMPORT_READY';
    `);
    await queryRunner.query(`
      ALTER TABLE freight.locomotives
      ADD COLUMN IF NOT EXISTS readiness VARCHAR(20) NOT NULL DEFAULT 'IMPORT_READY';
    `);

    await queryRunner.query(`
      CREATE INDEX IF NOT EXISTS idx_wagons_readiness
      ON freight.wagons (readiness)
      WHERE deleted_at IS NULL;
    `);
    await queryRunner.query(`
      CREATE INDEX IF NOT EXISTS idx_locomotives_readiness
      ON freight.locomotives (readiness)
      WHERE deleted_at IS NULL;
    `);

    await queryRunner.query(`DROP INDEX IF EXISTS freight."IDX_wagon_current_yard_id"`);
    await queryRunner.query(`DROP INDEX IF EXISTS freight."IDX_locomotive_current_yard_id"`);
    await queryRunner.query(`
      ALTER TABLE freight.wagons DROP CONSTRAINT IF EXISTS "FK_wagon_current_yard";
    `);
    await queryRunner.query(`
      ALTER TABLE freight.locomotives DROP CONSTRAINT IF EXISTS "FK_locomotive_current_yard";
    `);
    await queryRunner.query(`ALTER TABLE freight.wagons DROP COLUMN IF EXISTS "current_yard_id";`);
    await queryRunner.query(`ALTER TABLE freight.locomotives DROP COLUMN IF EXISTS "current_yard_id";`);
  }
}
