import { MigrationInterface, QueryRunner } from "typeorm";

/**
 * Add FREE and BUSY statuses to vehicle status enum.
 */
export class AddVehicleStatuses1880000000000 implements MigrationInterface {
  name = "AddVehicleStatuses1880000000000";

  public async up(queryRunner: QueryRunner): Promise<void> {
    // Create enum type if it doesn't exist
    await queryRunner.query(`
      DO $$
      BEGIN
        IF NOT EXISTS (SELECT 1 FROM pg_type WHERE typname = 'vehicles_status_enum' AND typnamespace = (SELECT oid FROM pg_namespace WHERE nspname = 'freight')) THEN
          CREATE TYPE freight.vehicles_status_enum AS ENUM ('ACTIVE', 'FREE', 'BUSY', 'MAINTENANCE', 'RETIRED', 'OUT_OF_SERVICE');
        ELSE
          -- Add values if enum already exists but doesn't have them
          ALTER TYPE freight.vehicles_status_enum ADD VALUE IF NOT EXISTS 'FREE' BEFORE 'MAINTENANCE';
          ALTER TYPE freight.vehicles_status_enum ADD VALUE IF NOT EXISTS 'BUSY' AFTER 'FREE';
        END IF;
      END $$;
    `);
  }

  public async down(_queryRunner: QueryRunner): Promise<void> {
    // Note: Postgres cannot drop individual enum values, so the down migration is a no-op
    // The enum values FREE and BUSY will remain but will be unused after downgrade
  }
}
