defmodule Orcasite.Repo.Migrations.CreateAudioImages 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(:audio_images, primary_key: false) do
      add :id, :uuid, null: false, default: fragment("gen_random_uuid()"), primary_key: true
      add :image_type, :text
      add :status, :text, default: "new"
      add :start_time, :utc_datetime_usec
      add :end_time, :utc_datetime_usec
      add :parameters, :map, default: %{}
      add :image_size, :bigint
      add :bucket, :text
      add :bucket_region, :text
      add :object_path, :text

      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 :feed_id,
          references(:feeds, column: :id, name: "audio_images_feed_id_fkey", type: :uuid)
    end

    create index(:audio_images, [:status])

    create index(:audio_images, [:image_type])

    create index(:audio_images, [:bucket])

    create index(:audio_images, [:feed_id])

    create index(:audio_images, [:end_time])

    create index(:audio_images, [:start_time])

    create table(:audio_image_feed_segments, 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 :audio_image_id,
          references(:audio_images,
            column: :id,
            name: "audio_image_feed_segments_audio_image_id_fkey",
            type: :uuid
          )

      add :feed_segment_id,
          references(:feed_segments,
            column: :id,
            name: "audio_image_feed_segments_feed_segment_id_fkey",
            type: :uuid
          )
    end

    create index(:audio_image_feed_segments, [:audio_image_id])

    create index(:audio_image_feed_segments, [:feed_segment_id])
  end

  def down do
    drop constraint(:audio_image_feed_segments, "audio_image_feed_segments_audio_image_id_fkey")

    drop constraint(:audio_image_feed_segments, "audio_image_feed_segments_feed_segment_id_fkey")

    drop_if_exists index(:audio_image_feed_segments, [:feed_segment_id])

    drop_if_exists index(:audio_image_feed_segments, [:audio_image_id])

    drop table(:audio_image_feed_segments)

    drop constraint(:audio_images, "audio_images_feed_id_fkey")

    drop_if_exists index(:audio_images, [:start_time])

    drop_if_exists index(:audio_images, [:end_time])

    drop_if_exists index(:audio_images, [:feed_id])

    drop_if_exists index(:audio_images, [:bucket])

    drop_if_exists index(:audio_images, [:image_type])

    drop_if_exists index(:audio_images, [:status])

    drop table(:audio_images)
  end
end
