# SPDX-FileCopyrightText: 2022 Alembic Pty Ltd
#
# SPDX-License-Identifier: MIT

defmodule Example.Repo.Migrations.AddingExampleMultiTenant 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(:mt_user_with_token_required, primary_key: false) do
      add(:id, :uuid, null: false, default: fragment("gen_random_uuid()"), primary_key: true)
      add(:email, :citext, null: false)
      add(:hashed_password, :text)

      add(:created_at, :utc_datetime_usec,
        null: false,
        default: fragment("(now() AT TIME ZONE 'utc')")
      )

      add(:updated_at, :utc_datetime_usec,
        null: false,
        default: fragment("(now() AT TIME ZONE 'utc')")
      )

      add(:organisation_id, :uuid)
    end

    create table(:mt_user_with_register_magic_link, primary_key: false) do
      add(:confirmed_at, :utc_datetime_usec)
      add(:id, :uuid, null: false, default: fragment("gen_random_uuid()"), primary_key: true)
      add(:email, :citext, null: false)

      add(:created_at, :utc_datetime_usec,
        null: false,
        default: fragment("(now() AT TIME ZONE 'utc')")
      )

      add(:updated_at, :utc_datetime_usec,
        null: false,
        default: fragment("(now() AT TIME ZONE 'utc')")
      )

      add(:organisation_id, :uuid)
    end

    create table(:mt_user_identities, primary_key: false) do
      add(:refresh_token, :text)
      add(:access_token_expires_at, :utc_datetime_usec)
      add(:access_token, :text)
      add(:uid, :text, null: false)
      add(:strategy, :text, null: false)
      add(:id, :uuid, null: false, default: fragment("gen_random_uuid()"), primary_key: true)
      add(:user_id, :uuid)
      add(:organisation_id, :uuid)
    end

    create table(:mt_user, primary_key: false) do
      add(:confirmed_at, :utc_datetime_usec)
      add(:id, :uuid, null: false, default: fragment("gen_random_uuid()"), primary_key: true)
    end

    alter table(:mt_user_identities) do
      modify(
        :user_id,
        references(:mt_user,
          column: :id,
          name: "mt_user_identities_user_id_fkey",
          type: :uuid,
          prefix: "public"
        )
      )
    end

    alter table(:mt_user) do
      add(:username, :citext, null: false)
      add(:extra_stuff, :text)
      add(:not_accepted_extra_stuff, :text)
      add(:hashed_password, :text)

      add(:created_at, :utc_datetime_usec,
        null: false,
        default: fragment("(now() AT TIME ZONE 'utc')")
      )

      add(:updated_at, :utc_datetime_usec,
        null: false,
        default: fragment("(now() AT TIME ZONE 'utc')")
      )

      add(:organisation_id, :uuid)
    end

    create table(:mt_tokens, primary_key: false) do
      add(:updated_at, :utc_datetime_usec,
        null: false,
        default: fragment("(now() AT TIME ZONE 'utc')")
      )

      add(:created_at, :utc_datetime_usec,
        null: false,
        default: fragment("(now() AT TIME ZONE 'utc')")
      )

      add(:extra_data, :map)
      add(:purpose, :text, null: false)
      add(:expires_at, :utc_datetime, null: false)
      add(:subject, :text, null: false)
      add(:jti, :text, null: false, primary_key: true)
      add(:organisation_id, :uuid)
    end

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

    alter table(:mt_user_with_token_required) do
      modify(
        :organisation_id,
        references(:mt_organisations,
          column: :id,
          name: "mt_user_with_token_required_organisation_id_fkey",
          type: :uuid,
          prefix: "public"
        )
      )
    end

    create unique_index(:mt_user_with_token_required, [:email],
             name: "mt_user_with_token_required_email_index"
           )

    alter table(:mt_user_with_register_magic_link) do
      modify(
        :organisation_id,
        references(:mt_organisations,
          column: :id,
          name: "mt_user_with_register_magic_link_organisation_id_fkey",
          type: :uuid,
          prefix: "public"
        )
      )
    end

    create unique_index(:mt_user_with_register_magic_link, [:email],
             name: "mt_user_with_register_magic_link_email_index"
           )

    alter table(:mt_user_identities) do
      modify(
        :organisation_id,
        references(:mt_organisations,
          column: :id,
          name: "mt_user_identities_organisation_id_fkey",
          type: :uuid,
          prefix: "public"
        )
      )
    end

    create unique_index(:mt_user_identities, [:strategy, :uid, :user_id],
             name: "mt_user_identities_unique_on_strategy_and_uid_and_user_id_index"
           )

    alter table(:mt_user) do
      modify(
        :organisation_id,
        references(:mt_organisations,
          column: :id,
          name: "mt_user_organisation_id_fkey",
          type: :uuid,
          prefix: "public"
        )
      )
    end

    create unique_index(:mt_user, [:username], name: "mt_user_username_index")

    alter table(:mt_tokens) do
      modify(
        :organisation_id,
        references(:mt_organisations,
          column: :id,
          name: "mt_tokens_organisation_id_fkey",
          type: :uuid,
          prefix: "public"
        )
      )
    end

    alter table(:mt_organisations) do
      add(:name, :citext, null: false)

      add(:created_at, :utc_datetime_usec,
        null: false,
        default: fragment("(now() AT TIME ZONE 'utc')")
      )

      add(:updated_at, :utc_datetime_usec,
        null: false,
        default: fragment("(now() AT TIME ZONE 'utc')")
      )
    end
  end

  def down do
    alter table(:mt_organisations) do
      remove(:updated_at)
      remove(:created_at)
      remove(:name)
    end

    drop(constraint(:mt_tokens, "mt_tokens_organisation_id_fkey"))

    alter table(:mt_tokens) do
      modify(:organisation_id, :uuid)
    end

    drop_if_exists(unique_index(:mt_user, [:username], name: "mt_user_username_index"))

    drop(constraint(:mt_user, "mt_user_organisation_id_fkey"))

    alter table(:mt_user) do
      modify(:organisation_id, :uuid)
    end

    drop_if_exists(
      unique_index(:mt_user_identities, [:strategy, :uid, :user_id],
        name: "mt_user_identities_unique_on_strategy_and_uid_and_user_id_index"
      )
    )

    drop(constraint(:mt_user_identities, "mt_user_identities_organisation_id_fkey"))

    alter table(:mt_user_identities) do
      modify(:organisation_id, :uuid)
    end

    drop_if_exists(
      unique_index(:mt_user_with_register_magic_link, [:email],
        name: "mt_user_with_register_magic_link_email_index"
      )
    )

    drop(
      constraint(
        :mt_user_with_register_magic_link,
        "mt_user_with_register_magic_link_organisation_id_fkey"
      )
    )

    alter table(:mt_user_with_register_magic_link) do
      modify(:organisation_id, :uuid)
    end

    drop_if_exists(
      unique_index(:mt_user_with_token_required, [:email],
        name: "mt_user_with_token_required_email_index"
      )
    )

    drop(
      constraint(:mt_user_with_token_required, "mt_user_with_token_required_organisation_id_fkey")
    )

    alter table(:mt_user_with_token_required) do
      modify(:organisation_id, :uuid)
    end

    drop(table(:mt_organisations))

    drop(table(:mt_tokens))

    alter table(:mt_user) do
      remove(:organisation_id)
      remove(:updated_at)
      remove(:created_at)
      remove(:hashed_password)
      remove(:not_accepted_extra_stuff)
      remove(:extra_stuff)
      remove(:username)
    end

    drop(constraint(:mt_user_identities, "mt_user_identities_user_id_fkey"))

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

    drop(table(:mt_user))

    drop(table(:mt_user_identities))

    drop(table(:mt_user_with_register_magic_link))

    drop(table(:mt_user_with_token_required))
  end
end
