defmodule Berrypod.Repo.Migrations.CreateReviews do
  use Ecto.Migration

  def change do
    create table(:reviews, primary_key: false) do
      add :id, :binary_id, primary_key: true

      add :product_id, references(:products, type: :binary_id, on_delete: :delete_all),
        null: false

      add :order_id, references(:orders, type: :binary_id, on_delete: :nilify_all)
      add :email, :string, null: false
      add :author_name, :string, null: false
      add :rating, :integer, null: false
      add :title, :string
      add :body, :text
      add :status, :string, null: false, default: "pending"
      add :image_ids, {:array, :binary_id}, default: []

      timestamps(type: :utc_datetime)
    end

    create unique_index(:reviews, [:email, :product_id])
    create index(:reviews, [:product_id, :status])
    create index(:reviews, [:status])
  end
end
