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

defmodule Demo.Repo.Migrations.AddMissingFields 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
    alter table(:users) do
      modify :updated_at, :utc_datetime_usec, null: false
      modify :inserted_at, :utc_datetime_usec, null: false
      modify :admin, :boolean, null: false
      modify :representative, :boolean
      modify :last_name, :text
      modify :first_name, :text
      modify :id, :uuid
    end

    execute(
      "ALTER INDEX users_representative_name_unique_index RENAME TO users_representative_name_index"
    )

    drop constraint(:tickets, "tickets_organization_id_fkey")

    drop constraint(:tickets, "tickets_representative_id_fkey")

    drop constraint(:tickets, "tickets_reporter_id_fkey")

    alter table(:tickets) do
      modify :status, :text, default: "new"

      modify :reporter_id,
             references(:users,
               column: :id,
               name: "tickets_reporter_id_fkey",
               type: :uuid,
               prefix: "public"
             )
    end

    drop constraint(:ticket_links, "ticket_links_destination_id_fkey")

    drop constraint(:ticket_links, "ticket_links_source_id_fkey")

    alter table(:ticket_links) do
      modify :source_id,
             references(:tickets,
               column: :id,
               name: "ticket_links_source_id_fkey",
               type: :uuid,
               prefix: "public"
             )
    end

    drop constraint(:ticket_comments, "ticket_comments_commenting_representative_id_fkey")

    drop constraint(:ticket_comments, "ticket_comments_commenting_customer_id_fkey")

    drop constraint(:ticket_comments, "ticket_comments_resource_id_fkey")

    alter table(:ticket_comments) do
      modify :resource_id,
             references(:tickets,
               column: :id,
               name: "ticket_comments_resource_id_fkey",
               type: :uuid
             )
    end

    drop constraint(
           :representative_comments,
           "representative_comments_commenting_representative_id_fkey"
         )

    drop constraint(
           :representative_comments,
           "representative_comments_commenting_customer_id_fkey"
         )

    drop constraint(:representative_comments, "representative_comments_resource_id_fkey")

    alter table(:representative_comments) do
      modify :resource_id,
             references(:users,
               column: :id,
               name: "representative_comments_resource_id_fkey",
               type: :uuid
             )
    end

    execute(
      "ALTER TABLE representative_comments alter CONSTRAINT representative_comments_resource_id_fkey NOT DEFERRABLE"
    )

    alter table(:representative_comments) do
      modify :commenting_customer_id,
             references(:users,
               column: :id,
               name: "representative_comments_commenting_customer_id_fkey",
               type: :uuid,
               prefix: "public"
             )
    end

    execute(
      "ALTER TABLE representative_comments alter CONSTRAINT representative_comments_commenting_customer_id_fkey NOT DEFERRABLE"
    )

    alter table(:representative_comments) do
      modify :commenting_representative_id,
             references(:users,
               column: :id,
               name: "representative_comments_commenting_representative_id_fkey",
               type: :uuid,
               prefix: "public"
             )
    end

    execute(
      "ALTER TABLE representative_comments alter CONSTRAINT representative_comments_commenting_representative_id_fkey NOT DEFERRABLE"
    )

    execute(
      "ALTER TABLE ticket_comments alter CONSTRAINT ticket_comments_resource_id_fkey NOT DEFERRABLE"
    )

    alter table(:ticket_comments) do
      modify :commenting_customer_id,
             references(:users,
               column: :id,
               name: "ticket_comments_commenting_customer_id_fkey",
               type: :uuid,
               prefix: "public"
             )
    end

    execute(
      "ALTER TABLE ticket_comments alter CONSTRAINT ticket_comments_commenting_customer_id_fkey NOT DEFERRABLE"
    )

    alter table(:ticket_comments) do
      modify :commenting_representative_id,
             references(:users,
               column: :id,
               name: "ticket_comments_commenting_representative_id_fkey",
               type: :uuid,
               prefix: "public"
             )
    end

    execute(
      "ALTER TABLE ticket_comments alter CONSTRAINT ticket_comments_commenting_representative_id_fkey NOT DEFERRABLE"
    )

    execute(
      "ALTER TABLE ticket_links alter CONSTRAINT ticket_links_source_id_fkey NOT DEFERRABLE"
    )

    alter table(:ticket_links) do
      modify :destination_id,
             references(:tickets,
               column: :id,
               name: "ticket_links_destination_id_fkey",
               type: :uuid,
               prefix: "public"
             )
    end

    execute(
      "ALTER TABLE ticket_links alter CONSTRAINT ticket_links_destination_id_fkey NOT DEFERRABLE"
    )

    execute("ALTER TABLE tickets alter CONSTRAINT tickets_reporter_id_fkey NOT DEFERRABLE")

    alter table(:tickets) do
      modify :representative_id,
             references(:users,
               column: :id,
               name: "tickets_representative_id_fkey",
               type: :uuid,
               prefix: "public"
             )
    end

    execute("ALTER TABLE tickets alter CONSTRAINT tickets_representative_id_fkey NOT DEFERRABLE")

    alter table(:tickets) do
      modify :organization_id,
             references(:organizations,
               column: :id,
               name: "tickets_organization_id_fkey",
               type: :uuid,
               prefix: "public"
             )
    end

    execute("ALTER TABLE tickets alter CONSTRAINT tickets_organization_id_fkey NOT DEFERRABLE")
  end

  def down do
    drop constraint(:tickets, "tickets_organization_id_fkey")

    alter table(:tickets) do
      modify :organization_id,
             references(:organizations,
               column: :id,
               name: "tickets_organization_id_fkey",
               type: :uuid
             )
    end

    drop constraint(:tickets, "tickets_representative_id_fkey")

    alter table(:tickets) do
      modify :representative_id,
             references(:users, column: :id, name: "tickets_representative_id_fkey", type: :uuid)
    end

    drop constraint(:ticket_links, "ticket_links_destination_id_fkey")

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

    drop constraint(:ticket_comments, "ticket_comments_commenting_representative_id_fkey")

    alter table(:ticket_comments) do
      modify :commenting_representative_id,
             references(:users,
               column: :id,
               name: "ticket_comments_commenting_representative_id_fkey",
               type: :uuid
             )
    end

    drop constraint(:ticket_comments, "ticket_comments_commenting_customer_id_fkey")

    alter table(:ticket_comments) do
      modify :commenting_customer_id,
             references(:users,
               column: :id,
               name: "ticket_comments_commenting_customer_id_fkey",
               type: :uuid
             )
    end

    drop constraint(
           :representative_comments,
           "representative_comments_commenting_representative_id_fkey"
         )

    alter table(:representative_comments) do
      modify :commenting_representative_id,
             references(:users,
               column: :id,
               name: "representative_comments_commenting_representative_id_fkey",
               type: :uuid
             )
    end

    drop constraint(
           :representative_comments,
           "representative_comments_commenting_customer_id_fkey"
         )

    alter table(:representative_comments) do
      modify :commenting_customer_id,
             references(:users,
               column: :id,
               name: "representative_comments_commenting_customer_id_fkey",
               type: :uuid
             )
    end

    drop constraint(:representative_comments, "representative_comments_resource_id_fkey")

    alter table(:representative_comments) do
      modify :resource_id,
             references(:users,
               column: :id,
               name: "representative_comments_resource_id_fkey",
               type: :uuid
             )
    end

    drop constraint(:ticket_comments, "ticket_comments_resource_id_fkey")

    alter table(:ticket_comments) do
      modify :resource_id,
             references(:tickets,
               column: :id,
               name: "ticket_comments_resource_id_fkey",
               type: :uuid
             )
    end

    drop constraint(:ticket_links, "ticket_links_source_id_fkey")

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

    drop constraint(:tickets, "tickets_reporter_id_fkey")

    alter table(:tickets) do
      modify :reporter_id,
             references(:users, column: :id, name: "tickets_reporter_id_fkey", type: :uuid)

      modify :status, :text, default: nil
    end

    execute(
      "ALTER INDEX users_representative_name_index RENAME TO users_representative_name_unique_index"
    )

    alter table(:users) do
      modify :id, :uuid
      modify :first_name, :text
      modify :last_name, :text
      modify :representative, :boolean
      modify :admin, :boolean, null: true
      modify :inserted_at, :utc_datetime_usec, null: true
      modify :updated_at, :utc_datetime_usec, null: true
    end
  end
end