defmodule Noted.Repo.Migrations.AddNotesTable 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(:notes, primary_key: false) do
      add :id, :uuid, null: false, default: fragment("uuid_generate_v7()"), primary_key: true
      add :title, :text, null: false
      add :content, :text, 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 :team_member_id,
          references(:team_members,
            column: :id,
            name: "notes_team_member_id_fkey",
            type: :uuid,
            prefix: "public",
            on_delete: :delete_all
          ),
          null: false

      add :team_id,
          references(:teams,
            column: :id,
            name: "notes_team_id_fkey",
            type: :uuid,
            prefix: "public",
            on_delete: :delete_all
          ),
          null: false
    end

    create index(:notes, [:team_id, :team_member_id])

    create index(:notes, [:team_id])
  end

  def down do
    drop_if_exists index(:notes, [:team_id])

    drop_if_exists index(:notes, [:team_id, :team_member_id])

    drop constraint(:notes, "notes_team_member_id_fkey")

    drop constraint(:notes, "notes_team_id_fkey")

    drop table(:notes)
  end
end
