defmodule Orcasite.Repo.Migrations.CreateBoutsAndFeedStreams 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(:feed_streams, primary_key: false) do
      add :id, :uuid, null: false, default: fragment("uuid_generate_v7()"), primary_key: true
      add :start_time, :utc_datetime
      add :end_time, :utc_datetime
      add :duration, :decimal
      add :bucket_url, :text
      add :playlist_path, :text
      add :playlist_timestamp, :text
      add :playlist_m3u8_path, :text
      add :inserted_at, :utc_datetime_usec, null: false, default: fragment("now()")
      add :updated_at, :utc_datetime_usec, null: false, default: fragment("now()")

      add :feed_id,
          references(:feeds, column: :id, name: "feed_streams_feed_id_fkey", type: :uuid)

      add :prev_feed_stream_id,
          references(:feed_streams,
            column: :id,
            name: "feed_streams_prev_feed_stream_id_fkey",
            type: :uuid
          )

      add :next_feed_stream_id,
          references(:feed_streams,
            column: :id,
            name: "feed_streams_next_feed_stream_id_fkey",
            type: :uuid
          )
    end

    create index(:feed_streams, [:next_feed_stream_id])

    create index(:feed_streams, [:prev_feed_stream_id])

    create index(:feed_streams, [:feed_id])

    create index(:feed_streams, [:end_time])

    create index(:feed_streams, [:start_time])

    create table(:bouts, primary_key: false) do
      add :id, :uuid, null: false, default: fragment("uuid_generate_v7()"), primary_key: true
      add :start_time, :utc_datetime
      add :end_time, :utc_datetime
      add :duration, :decimal
      add :ongoing, :boolean
      add :category, :text
      add :inserted_at, :utc_datetime_usec, null: false, default: fragment("now()")
      add :updated_at, :utc_datetime_usec, null: false, default: fragment("now()")
      add :feed_id, references(:feeds, column: :id, name: "bouts_feed_id_fkey", type: :uuid)

      add :created_by_user_id,
          references(:users, column: :id, name: "bouts_created_by_user_id_fkey", type: :uuid)
    end

    create index(:bouts, [:created_by_user_id])

    create index(:bouts, [:feed_id])

    create table(:bout_feed_streams, primary_key: false) do
      add :id, :uuid, null: false, primary_key: true
      add :inserted_at, :utc_datetime_usec, null: false, default: fragment("now()")
      add :updated_at, :utc_datetime_usec, null: false, default: fragment("now()")

      add :bout_id,
          references(:bouts, column: :id, name: "bout_feed_streams_bout_id_fkey", type: :uuid)

      add :feed_stream_id,
          references(:feed_streams,
            column: :id,
            name: "bout_feed_streams_feed_stream_id_fkey",
            type: :uuid
          )
    end

    create index(:bout_feed_streams, [:feed_stream_id])

    create index(:bout_feed_streams, [:bout_id])
  end

  def down do
    drop constraint(:bout_feed_streams, "bout_feed_streams_bout_id_fkey")

    drop constraint(:bout_feed_streams, "bout_feed_streams_feed_stream_id_fkey")

    drop_if_exists index(:bout_feed_streams, [:bout_id])

    drop_if_exists index(:bout_feed_streams, [:feed_stream_id])

    drop table(:bout_feed_streams)

    drop constraint(:bouts, "bouts_feed_id_fkey")

    drop constraint(:bouts, "bouts_created_by_user_id_fkey")

    drop_if_exists index(:bouts, [:feed_id])

    drop_if_exists index(:bouts, [:created_by_user_id])

    drop table(:bouts)

    drop constraint(:feed_streams, "feed_streams_feed_id_fkey")

    drop constraint(:feed_streams, "feed_streams_prev_feed_stream_id_fkey")

    drop constraint(:feed_streams, "feed_streams_next_feed_stream_id_fkey")

    drop_if_exists index(:feed_streams, [:start_time])

    drop_if_exists index(:feed_streams, [:end_time])

    drop_if_exists index(:feed_streams, [:feed_id])

    drop_if_exists index(:feed_streams, [:prev_feed_stream_id])

    drop_if_exists index(:feed_streams, [:next_feed_stream_id])

    drop table(:feed_streams)
  end
end
