# SPDX-FileCopyrightText: 2020 Zach Daniel
#
# SPDX-License-Identifier: MIT

defmodule AshPostgres.TestRepo.Migrations.MigrateResources1 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
    execute("""
    CREATE TYPE status AS ENUM ('open', 'closed');
    """
    )

    execute "CREATE SCHEMA profiles"

    create table(:users, primary_key: false) do
      add :id, :uuid, null: false, default: fragment("uuid_generate_v4()"), primary_key: true
      add :name, :text
      add :org_id, :uuid
    end

    create table(:profile, primary_key: false, prefix: "profiles") do
      add :id, :uuid, null: false, default: fragment("uuid_generate_v4()"), primary_key: true
      add :description, :text
      add :author_id, :uuid
    end

    create table(:posts, primary_key: false) do
      add :id, :uuid, null: false, default: fragment("uuid_generate_v4()"), primary_key: true
      add :title, :text
      add :score, :bigint
      add :public, :boolean
      add :category, :citext
      add :type, :text, default: "sponsored"
      add :price, :bigint
      add :decimal, :decimal, default: "0"
      add :status, :text
      add :status_enum, :status
      add :point, {:array, :float}
      add :uniq_one, :text
      add :uniq_two, :text
      add :created_at, :utc_datetime_usec, null: false, default: fragment("now()")
      add :updated_at, :utc_datetime_usec, null: false, default: fragment("now()")
      add :author_id, :uuid
    end

    create table(:post_ratings, primary_key: false) do
      add :id, :uuid, null: false, default: fragment("uuid_generate_v4()"), primary_key: true
      add :score, :bigint

      add :resource_id,
          references(:posts, column: :id, name: "post_ratings_resource_id_fkey", type: :uuid)
    end

    create table(:post_links, primary_key: false) do
      add :source_post_id,
          references(:posts,
            column: :id,
            name: "post_links_source_post_id_fkey",
            type: :uuid,
            prefix: "public"
          ),
          primary_key: true,
          null: false

      add :destination_post_id,
          references(:posts,
            column: :id,
            name: "post_links_destination_post_id_fkey",
            type: :uuid,
            prefix: "public"
          ),
          primary_key: true,
          null: false
    end

    create table(:multitenant_orgs, primary_key: false) do
      add :id, :uuid, null: false, default: fragment("uuid_generate_v4()"), primary_key: true
    end

    alter table(:users) do
      modify :org_id,
             references(:multitenant_orgs,
               column: :id,
               name: "users_org_id_fkey",
               type: :uuid,
               prefix: "public"
             )
    end

    alter table(:multitenant_orgs) do
      add :name, :text
    end

    create unique_index(:multitenant_orgs, [:id, :name],
             name: "multitenant_orgs_unique_by_name_index"
           )

    create table(:integer_posts, primary_key: false) do
      add :id, :bigserial, null: false, primary_key: true
      add :title, :text
    end

    create table(:comments, primary_key: false) do
      add :id, :uuid, null: false, default: fragment("uuid_generate_v4()"), primary_key: true
      add :title, :text
      add :likes, :bigint
      add :arbitrary_timestamp, :utc_datetime_usec
      add :created_at, :utc_datetime_usec, null: false, default: fragment("now()")

      add :post_id,
          references(:posts,
            column: :id,
            name: "special_name_fkey",
            type: :uuid,
            prefix: "public",
            on_delete: :delete_all,
            on_update: :update_all
          )

      add :author_id, :uuid
    end

    create table(:comment_ratings, primary_key: false) do
      add :id, :uuid, null: false, default: fragment("uuid_generate_v4()"), primary_key: true
      add :score, :bigint

      add :resource_id,
          references(:comments, column: :id, name: "comment_ratings_resource_id_fkey", type: :uuid)
    end

    create table(:authors, primary_key: false) do
      add :id, :uuid, null: false, default: fragment("uuid_generate_v4()"), primary_key: true
    end

    alter table(:profile, prefix: "profiles") do
      modify :author_id,
             references(:authors,
               column: :id,
               prefix: "public",
               name: "profile_author_id_fkey",
               type: :uuid
             )
    end

    alter table(:posts) do
      modify :author_id,
             references(:authors,
               column: :id,
               prefix: "public",
               name: "posts_author_id_fkey",
               type: :uuid
             )
    end

    create unique_index(:posts, [:uniq_one, :uniq_two],
             where: "type = 'sponsored'",
             name: "posts_uniq_one_and_two_index"
           )

    alter table(:comments) do
      modify :author_id,
             references(:authors,
               column: :id,
               prefix: "public",
               name: "comments_author_id_fkey",
               type: :uuid
             )
    end

    alter table(:authors) do
      add :first_name, :text
      add :last_name, :text
      add :bio, :map
    end

    create constraint(:posts, :price_must_be_positive, check: "type = 'sponsored' AND price > 0")
  end

  def down do
    drop_if_exists constraint(:posts, :price_must_be_positive)

    alter table(:authors) do
      remove :bio
      remove :last_name
      remove :first_name
    end

    drop constraint(:comments, "comments_author_id_fkey")

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

    drop_if_exists unique_index(:posts, [:uniq_one, :uniq_two],
                     name: "posts_uniq_one_and_two_index"
                   )

    drop constraint(:posts, "posts_author_id_fkey")

    alter table(:posts) do
      modify :author_id, :uuid
    end

    drop constraint(:profile, "profile_author_id_fkey", prefix: "profiles")

    alter table(:profile, prefix: "profiles") do
      modify :author_id, :uuid
    end

    drop table(:authors)

    drop constraint(:comment_ratings, "comment_ratings_resource_id_fkey")

    drop table(:comment_ratings)

    drop constraint(:comments, "special_name_fkey")

    drop table(:comments)

    drop table(:integer_posts)

    drop_if_exists unique_index(:multitenant_orgs, [:id, :name],
                     name: "multitenant_orgs_unique_by_name_index"
                   )

    alter table(:multitenant_orgs) do
      remove :name
    end

    drop constraint(:users, "users_org_id_fkey")

    alter table(:users) do
      modify :org_id, :uuid
    end

    drop table(:multitenant_orgs)

    drop constraint(:post_links, "post_links_destination_post_id_fkey")

    drop constraint(:post_links, "post_links_source_post_id_fkey")

    drop table(:post_links)

    drop constraint(:post_ratings, "post_ratings_resource_id_fkey")

    drop table(:post_ratings)

    drop table(:posts)

    drop table(:profile, prefix: "profiles")

    drop table(:users)
    execute("""
    DROP TYPE status;
    """)

    execute "DROP SCHEMA profiles"
  end
end
