import { MigrationInterface, QueryRunner } from 'typeorm';

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

  public async up(queryRunner: QueryRunner): Promise<void> {
    await queryRunner.query(`
      ALTER TABLE freight.locomotives
      ADD COLUMN IF NOT EXISTS locomotive_type VARCHAR(20) NOT NULL DEFAULT 'DIESEL',
      ADD COLUMN IF NOT EXISTS max_train_length_meters NUMERIC(10,3) NOT NULL DEFAULT 760,
      ADD COLUMN IF NOT EXISTS power_kw NUMERIC(10,3) NULL,
      ADD COLUMN IF NOT EXISTS traction_force_kn NUMERIC(10,3) NULL,
      ADD COLUMN IF NOT EXISTS max_speed_kmh NUMERIC(10,3) NULL;
    `);

    await queryRunner.query(`
      UPDATE freight.locomotives
      SET status = 'OUT_OF_SERVICE'
      WHERE status = 'INACTIVE';
    `);

    await queryRunner.query(`
      CREATE TABLE IF NOT EXISTS freight.routes (
        id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
        name VARCHAR(120) NOT NULL UNIQUE,
        origin_yard_id UUID NOT NULL REFERENCES freight.yards(id),
        destination_yard_id UUID NOT NULL REFERENCES freight.yards(id),
        is_active BOOLEAN NOT NULL DEFAULT true,
        created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
        updated_at TIMESTAMPTZ NOT NULL DEFAULT now(),
        deleted_at TIMESTAMPTZ NULL
      );
    `);

    await queryRunner.query(`
      CREATE TABLE IF NOT EXISTS freight.route_milestones (
        id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
        route_id UUID NOT NULL REFERENCES freight.routes(id) ON DELETE CASCADE,
        yard_id UUID NOT NULL REFERENCES freight.yards(id),
        sequence_no INT NOT NULL,
        created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
        updated_at TIMESTAMPTZ NOT NULL DEFAULT now(),
        deleted_at TIMESTAMPTZ NULL,
        CONSTRAINT uq_route_milestones_route_sequence UNIQUE (route_id, sequence_no)
      );
    `);

    await queryRunner.query(`
      CREATE INDEX IF NOT EXISTS idx_routes_origin_yard_id
      ON freight.routes(origin_yard_id);
    `);
    await queryRunner.query(`
      CREATE INDEX IF NOT EXISTS idx_routes_destination_yard_id
      ON freight.routes(destination_yard_id);
    `);
    await queryRunner.query(`
      CREATE INDEX IF NOT EXISTS idx_routes_is_active
      ON freight.routes(is_active);
    `);
    await queryRunner.query(`
      CREATE INDEX IF NOT EXISTS idx_route_milestones_route_id
      ON freight.route_milestones(route_id);
    `);
    await queryRunner.query(`
      CREATE INDEX IF NOT EXISTS idx_route_milestones_yard_id
      ON freight.route_milestones(yard_id);
    `);
  }

  public async down(queryRunner: QueryRunner): Promise<void> {
    await queryRunner.query(`DROP TABLE IF EXISTS freight.route_milestones;`);
    await queryRunner.query(`DROP TABLE IF EXISTS freight.routes;`);
    await queryRunner.query(`
      ALTER TABLE freight.locomotives
      DROP COLUMN IF EXISTS max_speed_kmh,
      DROP COLUMN IF EXISTS traction_force_kn,
      DROP COLUMN IF EXISTS power_kw,
      DROP COLUMN IF EXISTS max_train_length_meters,
      DROP COLUMN IF EXISTS locomotive_type;
    `);
    await queryRunner.query(`
      UPDATE freight.locomotives
      SET status = 'INACTIVE'
      WHERE status = 'OUT_OF_SERVICE';
    `);
  }
}
