# typed: strict
class ActiveRecord::Migration
  extend T::Sig

  sig do
    params(
      table_name: T.any(String, Symbol),
      column_name: T.any(String, Symbol),
      column_type: T.any(String, Symbol),
      options: T::Hash[Symbol, T.untyped],
    ).void
  end
  def add_json_index(table_name, column_name, column_type, options = {})
    add_index(
      table_name,
      "((json_attributes->>'#{column_name}')::#{column_type})",
      name: "idx_#{table_name}_on_#{column_name}",
      **options,
    )
  end

  sig { params(name: String, values: T::Array[String]).void }
  def create_enum(name, values)
    reversible do |dir|
      dir.up do
        execute "CREATE TYPE #{name} AS ENUM (#{values.map { |t| "'#{t}'" }.join(",")})"
      end

      dir.down { execute "DROP TYPE #{name}" }
    end
  end

  sig do
    params(enum_name: Symbol, values: T.class_of(ReduxApplicationRecord)).void
  end
  def add_enum_value(enum_name, *values)
    up_only do
      values.each do |value|
        execute "ALTER TYPE #{enum_name} ADD VALUE IF NOT EXISTS '#{value.name}'"
      end
    end
  end

  sig { params(both_ways: T::Boolean).void }
  def mirai_tablespace!(both_ways: false)
    reversible do |dir|
      dir.up { execute "SET DEFAULT_TABLESPACE = mirai" }
      dir.down { execute "SET DEFAULT_TABLESPACE = mirai" } if both_ways
    end
  end
end
