defmodule Mv.Repo.Migrations.RemoveBirthDateFromMembers do
  @moduledoc """
  Removes the birth_date column from the members table.

  The birth_date field has been removed from the application because most users
  don't record birthday data. Users who need this can use a custom field instead.

  This migration also updates the search_vector trigger to remove birth_date.
  """

  use Ecto.Migration

  def up do
    # Update the trigger function to remove birth_date from search_vector
    execute("""
    CREATE OR REPLACE FUNCTION members_search_vector_trigger() RETURNS trigger AS $$
    BEGIN
      NEW.search_vector :=
        setweight(to_tsvector('simple', coalesce(NEW.first_name, '')), 'A') ||
        setweight(to_tsvector('simple', coalesce(NEW.last_name, '')), 'A') ||
        setweight(to_tsvector('simple', coalesce(NEW.email, '')), 'B') ||
        setweight(to_tsvector('simple', coalesce(NEW.phone_number, '')), 'C') ||
        setweight(to_tsvector('simple', coalesce(NEW.join_date::text, '')), 'D') ||
        setweight(to_tsvector('simple', coalesce(NEW.exit_date::text, '')), 'D') ||
        setweight(to_tsvector('simple', coalesce(NEW.notes, '')), 'B') ||
        setweight(to_tsvector('simple', coalesce(NEW.city, '')), 'C') ||
        setweight(to_tsvector('simple', coalesce(NEW.street, '')), 'C') ||
        setweight(to_tsvector('simple', coalesce(NEW.house_number::text, '')), 'C') ||
        setweight(to_tsvector('simple', coalesce(NEW.postal_code::text, '')), 'C');
      RETURN NEW;
    END
    $$ LANGUAGE plpgsql;
    """)

    # Remove the birth_date column
    alter table(:members) do
      remove :birth_date
    end
  end

  def down do
    # Add the birth_date column back
    alter table(:members) do
      add :birth_date, :date
    end

    # Restore the trigger function with birth_date
    execute("""
    CREATE OR REPLACE FUNCTION members_search_vector_trigger() RETURNS trigger AS $$
    BEGIN
      NEW.search_vector :=
        setweight(to_tsvector('simple', coalesce(NEW.first_name, '')), 'A') ||
        setweight(to_tsvector('simple', coalesce(NEW.last_name, '')), 'A') ||
        setweight(to_tsvector('simple', coalesce(NEW.email, '')), 'B') ||
        setweight(to_tsvector('simple', coalesce(NEW.birth_date::text, '')), 'C') ||
        setweight(to_tsvector('simple', coalesce(NEW.phone_number, '')), 'C') ||
        setweight(to_tsvector('simple', coalesce(NEW.join_date::text, '')), 'D') ||
        setweight(to_tsvector('simple', coalesce(NEW.exit_date::text, '')), 'D') ||
        setweight(to_tsvector('simple', coalesce(NEW.notes, '')), 'B') ||
        setweight(to_tsvector('simple', coalesce(NEW.city, '')), 'C') ||
        setweight(to_tsvector('simple', coalesce(NEW.street, '')), 'C') ||
        setweight(to_tsvector('simple', coalesce(NEW.house_number::text, '')), 'C') ||
        setweight(to_tsvector('simple', coalesce(NEW.postal_code::text, '')), 'C');
      RETURN NEW;
    END
    $$ LANGUAGE plpgsql;
    """)
  end
end
