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

defmodule AshPostgres.TestRepo.Migrations.MigrateResources48 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(:staff_group, primary_key: false) do
      add(:id, :uuid, null: false, default: fragment("gen_random_uuid()"), primary_key: true)

      add(:inserted_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

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

      add(:inserted_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(:note_id, :uuid)
    end

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

    alter table(:content) do
      modify(
        :note_id,
        references(:note,
          column: :id,
          name: "content_note_id_fkey",
          type: :uuid,
          prefix: "public"
        )
      )
    end

    alter table(:note) do
      add(:body, :text, null: false)

      add(:inserted_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

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

      add(
        :staff_group_id,
        references(:staff_group,
          column: :id,
          name: "staff_group_member_staff_group_id_fkey",
          type: :uuid,
          prefix: "public"
        ),
        primary_key: true,
        null: false
      )

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

    create table(:content_visibility_group, primary_key: false) do
      add(
        :content_id,
        references(:content,
          column: :id,
          name: "content_visibility_group_content_id_fkey",
          type: :uuid,
          prefix: "public"
        ),
        primary_key: true,
        null: false
      )

      add(
        :staff_group_id,
        references(:staff_group,
          column: :id,
          name: "content_visibility_group_staff_group_id_fkey",
          type: :uuid,
          prefix: "public"
        ),
        primary_key: true,
        null: false
      )
    end
  end

  def down do
    drop(constraint(:content_visibility_group, "content_visibility_group_content_id_fkey"))

    drop(constraint(:content_visibility_group, "content_visibility_group_staff_group_id_fkey"))

    drop(table(:content_visibility_group))

    drop(constraint(:staff_group_member, "staff_group_member_staff_group_id_fkey"))

    drop(constraint(:staff_group_member, "staff_group_member_user_id_fkey"))

    drop(table(:staff_group_member))

    alter table(:note) do
      remove(:updated_at)
      remove(:inserted_at)
      remove(:body)
    end

    drop(constraint(:content, "content_note_id_fkey"))

    alter table(:content) do
      modify(:note_id, :uuid)
    end

    drop(table(:note))

    drop(table(:content))

    drop(table(:staff_group))
  end
end
