defmodule Tololo.Repo.Migrations.Products 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(:variants, primary_key: false) do
      add :id, :uuid, null: false, default: fragment("uuid_generate_v7()"), primary_key: true
      add :name, :text, null: false
      add :description, :text
      add :sku, :text, null: false
      add :images, {:array, :text}
      add :attributes, {:array, :map}

      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 :product_id, :uuid
    end

    create table(:variant_option, primary_key: false) do
      add :variant_id,
          references(:variants,
            column: :id,
            name: "variant_option_variant_id_fkey",
            type: :uuid,
            prefix: "public"
          ),
          primary_key: true,
          null: false

      add :option_value_id, :uuid, null: false, primary_key: true
    end

    create table(:types, primary_key: false) do
      add :id, :uuid, null: false, default: fragment("uuid_generate_v7()"), primary_key: true
      add :name, :text, null: false
      add :description, :text
      add :attributes, {:array, :map}

      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')")
    end

    create table(:products, primary_key: false) do
      add :id, :uuid, null: false, default: fragment("uuid_generate_v7()"), primary_key: true
    end

    alter table(:variants) do
      modify :product_id,
             references(:products,
               column: :id,
               name: "variants_product_id_fkey",
               type: :uuid,
               prefix: "public",
               on_delete: :delete_all
             )
    end

    alter table(:products) do
      add :name, :text, null: false
      add :description, :text, null: false
      add :sku, :text, null: false
      add :state, :text, null: false, default: "disabled"

      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 :type_id,
          references(:types,
            column: :id,
            name: "products_type_id_fkey",
            type: :uuid,
            prefix: "public"
          )
    end

    create table(:product_option, primary_key: false) do
      add :product_id,
          references(:products,
            column: :id,
            name: "product_option_product_id_fkey",
            type: :uuid,
            prefix: "public"
          ),
          primary_key: true,
          null: false

      add :option_id, :uuid, null: false, primary_key: true
    end

    create table(:options, primary_key: false) do
      add :id, :uuid, null: false, default: fragment("uuid_generate_v7()"), primary_key: true
    end

    alter table(:product_option) do
      modify :option_id,
             references(:options,
               column: :id,
               name: "product_option_option_id_fkey",
               type: :uuid,
               prefix: "public"
             )
    end

    alter table(:options) do
      add :name, :text, null: false
      add :description, :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 :product_id,
          references(:products,
            column: :id,
            name: "options_product_id_fkey",
            type: :uuid,
            prefix: "public"
          )
    end

    create table(:option_values, primary_key: false) do
      add :id, :uuid, null: false, default: fragment("uuid_generate_v7()"), primary_key: true
    end

    alter table(:variant_option) do
      modify :option_value_id,
             references(:option_values,
               column: :id,
               name: "variant_option_option_value_id_fkey",
               type: :uuid,
               prefix: "public"
             )
    end

    alter table(:option_values) do
      add :value, :text, null: false

      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 :option_id,
          references(:options,
            column: :id,
            name: "option_values_option_id_fkey",
            type: :uuid,
            prefix: "public"
          )

      add :variant_id,
          references(:variants,
            column: :id,
            name: "option_values_variant_id_fkey",
            type: :uuid,
            prefix: "public"
          )
    end

    create table(:collections, primary_key: false) do
      add :id, :uuid, null: false, default: fragment("uuid_generate_v7()"), primary_key: true
      add :name, :text, null: false
      add :description, :text
      add :image, :text
      add :is_brand, :boolean, null: false, default: false

      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')")
    end

    create table(:collection_product, primary_key: false) do
      add :product_id,
          references(:products,
            column: :id,
            name: "collection_product_product_id_fkey",
            type: :uuid,
            prefix: "public"
          ),
          primary_key: true,
          null: false

      add :collection_id,
          references(:collections,
            column: :id,
            name: "collection_product_collection_id_fkey",
            type: :uuid,
            prefix: "public"
          ),
          primary_key: true,
          null: false
    end
  end

  def down do
    drop constraint(:collection_product, "collection_product_product_id_fkey")

    drop constraint(:collection_product, "collection_product_collection_id_fkey")

    drop table(:collection_product)

    drop table(:collections)

    drop constraint(:option_values, "option_values_option_id_fkey")

    drop constraint(:option_values, "option_values_variant_id_fkey")

    alter table(:option_values) do
      remove :variant_id
      remove :option_id
      remove :updated_at
      remove :inserted_at
      remove :value
    end

    drop constraint(:variant_option, "variant_option_option_value_id_fkey")

    alter table(:variant_option) do
      modify :option_value_id, :uuid
    end

    drop table(:option_values)

    drop constraint(:options, "options_product_id_fkey")

    alter table(:options) do
      remove :product_id
      remove :updated_at
      remove :inserted_at
      remove :description
      remove :name
    end

    drop constraint(:product_option, "product_option_option_id_fkey")

    alter table(:product_option) do
      modify :option_id, :uuid
    end

    drop table(:options)

    drop constraint(:product_option, "product_option_product_id_fkey")

    drop table(:product_option)

    drop constraint(:products, "products_type_id_fkey")

    alter table(:products) do
      remove :type_id
      remove :updated_at
      remove :inserted_at
      remove :state
      remove :sku
      remove :description
      remove :name
    end

    drop constraint(:variants, "variants_product_id_fkey")

    alter table(:variants) do
      modify :product_id, :uuid
    end

    drop table(:products)

    drop table(:types)

    drop constraint(:variant_option, "variant_option_variant_id_fkey")

    drop table(:variant_option)

    drop table(:variants)
  end
end
