defmodule Mv.Repo.Migrations.AddReviewedByDisplayToJoinRequests do
  @moduledoc """
  Adds reviewed_by_display to join_requests for showing reviewer in UI without loading User.

  Backfills existing rows from users.email where reviewed_by_user_id is set.
  """

  use Ecto.Migration

  def up do
    alter table(:join_requests) do
      add :reviewed_by_display, :text
    end

    # Backfill from users.email for rows that have reviewed_by_user_id
    execute """
    UPDATE join_requests j
    SET reviewed_by_display = u.email
    FROM users u
    WHERE j.reviewed_by_user_id = u.id
      AND j.reviewed_by_user_id IS NOT NULL
    """
  end

  def down do
    alter table(:join_requests) do
      remove :reviewed_by_display
    end
  end
end
