defmodule Realworld.Repo.Migrations.AddArticles 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(:tags, primary_key: false) do
      add :id, :uuid, null: false, primary_key: true
      add :name, :text, null: false
      add :created_at, :utc_datetime_usec, null: false, default: fragment("now()")
      add :updated_at, :utc_datetime_usec, null: false, default: fragment("now()")
    end

    create table(:favorites, primary_key: false) do
      add :id, :uuid, null: false, primary_key: true
      add :created_at, :utc_datetime_usec, null: false, default: fragment("now()")
      add :updated_at, :utc_datetime_usec, null: false, default: fragment("now()")

      add :user_id,
          references(:users,
            column: :id,
            name: "favorites_user_id_fkey",
            type: :uuid,
            prefix: "public"
          ),
          null: false

      add :article_id, :uuid, null: false
    end

    create table(:comments, primary_key: false) do
      add :id, :uuid, null: false, primary_key: true
      add :body, :text, null: false
      add :created_at, :utc_datetime_usec, null: false, default: fragment("now()")
      add :updated_at, :utc_datetime_usec, null: false, default: fragment("now()")
      add :article_id, :uuid, null: false
      add :user_id, :uuid
    end

    create table(:articles, primary_key: false) do
      add :id, :uuid, null: false, primary_key: true
    end

    alter table(:favorites) do
      modify :article_id,
             references(:articles,
               column: :id,
               prefix: "public",
               name: "favorites_article_id_fkey",
               type: :uuid
             )
    end

    alter table(:comments) do
      modify :article_id,
             references(:articles,
               column: :id,
               prefix: "public",
               name: "comments_article_id_fkey",
               type: :uuid
             )
    end

    alter table(:comments) do
      modify :user_id,
             references(:users,
               column: :id,
               prefix: "public",
               name: "comments_user_id_fkey",
               type: :uuid
             )
    end

    alter table(:articles) do
      add :slug, :text, null: false
      add :title, :text, null: false
      add :description, :text, null: false
      add :body, :text, null: false
      add :created_at, :utc_datetime_usec, null: false, default: fragment("now()")
      add :updated_at, :utc_datetime_usec, null: false, default: fragment("now()")

      add :user_id,
          references(:users,
            column: :id,
            name: "articles_user_id_fkey",
            type: :uuid,
            prefix: "public"
          ),
          null: false
    end

    create table(:article_tags, primary_key: false) do
      add :id, :uuid, null: false, primary_key: true

      add :article_id,
          references(:articles,
            column: :id,
            name: "article_tags_article_id_fkey",
            type: :uuid,
            prefix: "public"
          ),
          null: false

      add :tag_id,
          references(:tags,
            column: :id,
            name: "article_tags_tag_id_fkey",
            type: :uuid,
            prefix: "public"
          ),
          null: false
    end
  end

  def down do
    drop constraint(:article_tags, "article_tags_tag_id_fkey")

    drop constraint(:article_tags, "article_tags_article_id_fkey")

    drop table(:article_tags)

    drop constraint(:articles, "articles_user_id_fkey")

    alter table(:articles) do
      remove :user_id
      remove :updated_at
      remove :created_at
      remove :body
      remove :description
      remove :title
      remove :slug
    end

    drop constraint(:comments, "comments_user_id_fkey")

    alter table(:comments) do
      modify :user_id, :uuid
    end

    drop constraint(:comments, "comments_article_id_fkey")

    alter table(:comments) do
      modify :article_id, :uuid
    end

    drop constraint(:favorites, "favorites_article_id_fkey")

    alter table(:favorites) do
      modify :article_id, :uuid
    end

    drop table(:articles)

    drop table(:comments)

    drop constraint(:favorites, "favorites_user_id_fkey")

    drop table(:favorites)

    drop table(:tags)
  end
end