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

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

      add :provider_connection_id,
          references(:provider_connections, type: :binary_id, on_delete: :delete_all), null: false

      add :provider_product_id, :string, null: false
      add :title, :string, null: false
      add :description, :text
      add :slug, :string, null: false
      add :status, :string, default: "active", null: false
      add :visible, :boolean, default: true, null: false
      add :category, :string
      add :provider_data, :map, default: %{}
      add :checksum, :string

      timestamps(type: :utc_datetime)
    end

    create unique_index(:products, [:slug])
    create unique_index(:products, [:provider_connection_id, :provider_product_id])
    create index(:products, [:status])
    create index(:products, [:visible])
    create index(:products, [:category])
  end
end
