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

defmodule Demo.Repo.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
    create table(:users, primary_key: false) do
      add :id, :uuid, null: false, default: fragment("uuid_generate_v4()"), primary_key: true
      add :updated_at, :utc_datetime_usec, default: fragment("now()")
      add :tags, {:array, :text}
      add :representative, :boolean
      add :profile, :map
      add :last_name, :text
      add :inserted_at, :utc_datetime_usec, default: fragment("now()")
      add :first_name, :text
    end

    create unique_index(:users, [:first_name, :last_name],
             name: "users_representative_name_unique_index",
             where: "representative = true"
           )

    alter table(:users) do
      add :alternate_profiles, {:array, :map}
      add :admin, :boolean, default: false
    end

    create table(:tickets, primary_key: false) do
      add :id, :uuid, null: false, default: fragment("uuid_generate_v4()"), primary_key: true
      add :updated_at, :utc_datetime_usec, null: false, default: fragment("now()")
      add :subject, :text, null: false
      add :status, :text, null: false
      add :response, :text
      add :representative_id, :uuid
      add :reporter_id, :uuid
      add :organization_id, :uuid, null: false
      add :inserted_at, :utc_datetime_usec, null: false, default: fragment("now()")
      add :description, :text
    end

    create table(:ticket_links, primary_key: false) do
      add :source_id, :uuid, null: false, primary_key: true
      add :destination_id, :uuid, null: false, primary_key: true
      add :type, :text, null: false
    end

    create table(:ticket_comments, primary_key: false) do
      add :id, :uuid, null: false, default: fragment("uuid_generate_v4()"), primary_key: true
      add :resource_id, :uuid, null: false
      add :commenting_representative_id, :uuid
      add :commenting_customer_id, :uuid
      add :comment, :text
    end

    create table(:representative_comments, primary_key: false) do
      add :id, :uuid, null: false, default: fragment("uuid_generate_v4()"), primary_key: true
      add :resource_id, :uuid, null: false
      add :commenting_representative_id, :uuid
      add :commenting_customer_id, :uuid
      add :comment, :text
    end

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

    alter table(:representative_comments) do
      modify :commenting_customer_id, references(:users, type: :uuid, column: :id)
      modify :commenting_representative_id, references(:users, type: :uuid, column: :id)
      modify :resource_id, references(:users, type: :uuid, column: :id)
    end

    alter table(:ticket_comments) do
      modify :commenting_customer_id, references(:users, type: :uuid, column: :id)
      modify :commenting_representative_id, references(:users, type: :uuid, column: :id)
      modify :resource_id, references(:tickets, type: :uuid, column: :id)
    end

    alter table(:ticket_links) do
      modify :destination_id, references(:tickets, type: :uuid, column: :id)
      modify :source_id, references(:tickets, type: :uuid, column: :id)
    end

    alter table(:tickets) do
      modify :organization_id, references(:organizations, type: :uuid, column: :id)
      modify :reporter_id, references(:users, type: :uuid, column: :id)
      modify :representative_id, references(:users, type: :uuid, column: :id)
    end
  end

  def down do
    alter table(:tickets) do
      modify :representative_id, :uuid
      modify :reporter_id, :uuid
      modify :organization_id, :uuid
    end

    alter table(:ticket_links) do
      modify :source_id, :uuid
      modify :destination_id, :uuid
    end

    alter table(:ticket_comments) do
      modify :resource_id, :uuid
      modify :commenting_representative_id, :uuid
      modify :commenting_customer_id, :uuid
    end

    alter table(:representative_comments) do
      modify :resource_id, :uuid
      modify :commenting_representative_id, :uuid
      modify :commenting_customer_id, :uuid
    end

    drop table(:organizations)

    drop table(:representative_comments)

    drop table(:ticket_comments)

    drop table(:ticket_links)

    drop table(:tickets)

    alter table(:users) do
      remove :admin
      remove :alternate_profiles
    end

    drop_if_exists unique_index(:users, [:first_name, :last_name],
                     name: "users_representative_name_unique_index"
                   )

    drop table(:users)
  end
end