defmodule AshHq.Repo.Migrations.MigrateResources43 do
  @moduledoc """
  Updates resources based on their most recent snapshots.

  This file was autogenerated with `mix ash_postgres.generate_migrations`
  """

  use Ecto.Migration

  def up do
    create table(:discord_threads, primary_key: false) do
      add(:id, :bigint, null: false, primary_key: true)
      add(:type, :bigint)
      add(:name, :text, null: false)
      add(:author, :text, null: false)
      add(:create_timestamp, :utc_datetime, null: false)
      add(:channel_id, :bigint, null: false)
    end

    create table(:discord_thread_tags, primary_key: false) do
      add(
        :thread_id,
        references(:discord_threads,
          column: :id,
          name: "discord_thread_tags_thread_id_fkey",
          type: :bigint,
          prefix: "public"
        ),
        primary_key: true,
        null: false
      )

      add(:tag_id, :bigint, null: false, primary_key: true)
    end

    create table(:discord_tags, primary_key: false) do
      add(:id, :bigint, null: false, primary_key: true)
    end

    alter table(:discord_thread_tags) do
      modify(
        :tag_id,
        references(:discord_tags,
          column: :id,
          prefix: "public",
          name: "discord_thread_tags_tag_id_fkey",
          type: :bigint
        )
      )
    end

    alter table(:discord_tags) do
      add(:name, :citext, null: false)
      add(:channel_id, :bigint)
    end

    create table(:discord_reactions, primary_key: false) do
      add(:id, :uuid, null: false, default: fragment("uuid_generate_v4()"), primary_key: true)
      add(:count, :bigint, null: false)
      add(:emoji, :text, null: false)
      add(:message_id, :bigint, null: false)
    end

    create table(:discord_messages, primary_key: false) do
      add(:id, :bigint, null: false, primary_key: true)
    end

    alter table(:discord_reactions) do
      modify(
        :message_id,
        references(:discord_messages,
          column: :id,
          prefix: "public",
          name: "discord_reactions_message_id_fkey",
          type: :bigint
        )
      )
    end

    create unique_index(:discord_reactions, [:emoji, :message_id],
             name: "discord_reactions_unique_message_emoji_index"
           )

    alter table(:discord_messages) do
      add(:author, :text, null: false)
      add(:content, :text)
      add(:content_html, :text)
      add(:timestamp, :utc_datetime, null: false)

      add(
        :thread_id,
        references(:discord_threads,
          column: :id,
          name: "discord_messages_thread_id_fkey",
          type: :bigint,
          prefix: "public"
        ),
        null: false
      )
    end

    create table(:discord_channels, primary_key: false) do
      add(:id, :bigint, null: false, primary_key: true)
    end

    alter table(:discord_threads) do
      modify(
        :channel_id,
        references(:discord_channels,
          column: :id,
          prefix: "public",
          name: "discord_threads_channel_id_fkey",
          type: :bigint
        )
      )
    end

    alter table(:discord_tags) do
      modify(
        :channel_id,
        references(:discord_channels,
          column: :id,
          prefix: "public",
          name: "discord_tags_channel_id_fkey",
          type: :bigint
        )
      )
    end

    create unique_index(:discord_tags, [:name, :channel_id],
             name: "discord_tags_unique_name_per_channel_index"
           )

    alter table(:discord_channels) do
      add(:name, :text, null: false)
      add(:order, :bigint, null: false)
    end

    create table(:discord_attachments, primary_key: false) do
      add(:id, :bigint, null: false, primary_key: true)
      add(:filename, :text)
      add(:size, :bigint)
      add(:url, :text)
      add(:proxy_url, :text)
      add(:height, :bigint)
      add(:width, :bigint)

      add(
        :message_id,
        references(:discord_messages,
          column: :id,
          name: "discord_attachments_message_id_fkey",
          type: :bigint,
          prefix: "public"
        ),
        null: false
      )
    end
  end

  def down do
    drop(constraint(:discord_attachments, "discord_attachments_message_id_fkey"))

    drop(table(:discord_attachments))

    alter table(:discord_channels) do
      remove(:order)
      remove(:name)
    end

    drop_if_exists(
      unique_index(:discord_tags, [:name, :channel_id],
        name: "discord_tags_unique_name_per_channel_index"
      )
    )

    drop(constraint(:discord_tags, "discord_tags_channel_id_fkey"))

    alter table(:discord_tags) do
      modify(:channel_id, :bigint)
    end

    drop(constraint(:discord_threads, "discord_threads_channel_id_fkey"))

    alter table(:discord_threads) do
      modify(:channel_id, :bigint)
    end

    drop(table(:discord_channels))

    drop(constraint(:discord_messages, "discord_messages_thread_id_fkey"))

    alter table(:discord_messages) do
      remove(:thread_id)
      remove(:timestamp)
      remove(:content_html)
      remove(:content)
      remove(:author)
    end

    drop_if_exists(
      unique_index(:discord_reactions, [:emoji, :message_id],
        name: "discord_reactions_unique_message_emoji_index"
      )
    )

    drop(constraint(:discord_reactions, "discord_reactions_message_id_fkey"))

    alter table(:discord_reactions) do
      modify(:message_id, :bigint)
    end

    drop(table(:discord_messages))

    drop(table(:discord_reactions))

    alter table(:discord_tags) do
      remove(:channel_id)
      remove(:name)
    end

    drop(constraint(:discord_thread_tags, "discord_thread_tags_tag_id_fkey"))

    alter table(:discord_thread_tags) do
      modify(:tag_id, :bigint)
    end

    drop(table(:discord_tags))

    drop(constraint(:discord_thread_tags, "discord_thread_tags_thread_id_fkey"))

    drop(table(:discord_thread_tags))

    drop(table(:discord_threads))
  end
end
