<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        // 1. Rename table productos_attributes to attribute
        Schema::rename('productos_attributes', 'attribute');

        // 2. Rename table productos_variantes_definiciones to productos_variantes_values
        Schema::rename('productos_variantes_definiciones', 'productos_variantes_values');

        // 3. Create products_attributes intermediate table
        Schema::create('products_attributes', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('product_id');
            $table->unsignedBigInteger('attribute_id');
            $table->timestamps();

            $table->foreign('product_id')
                ->references('id')
                ->on('productos')
                ->cascadeOnDelete();

            $table->foreign('attribute_id')
                ->references('id')
                ->on('attribute')
                ->cascadeOnDelete();

            $table->unique(['product_id', 'attribute_id']);
        });

        // 4. Point variant values to the product-attribute pivot instead of the base attribute
        Schema::table('productos_variantes_values', function (Blueprint $table) {
            $table->index('producto_variante_id', 'prod_var_values_variant_id_index');
        });

        Schema::table('productos_variantes_values', function (Blueprint $table) {
            if (Schema::getConnection()->getDriverName() !== 'sqlite') {
                $table->dropForeign('productos_variantes_definiciones_attribute_id_foreign');
            }
            $table->dropUnique('prod_var_def_variant_attr_unique');
        });

        Schema::table('productos_variantes_values', function (Blueprint $table) {
            $table->renameColumn('attribute_id', 'products_attribute_id');
        });

        Schema::table('productos_variantes_values', function (Blueprint $table) {
            $table->foreign('products_attribute_id')
                ->references('id')
                ->on('products_attributes')
                ->cascadeOnUpdate()
                ->cascadeOnDelete();

            $table->unique(
                ['producto_variante_id', 'products_attribute_id'],
                'prod_var_values_variant_product_attr_unique'
            );
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::table('productos_variantes_values', function (Blueprint $table) {
            if (Schema::getConnection()->getDriverName() !== 'sqlite') {
                $table->dropForeign(['products_attribute_id']);
            }
            $table->dropUnique('prod_var_values_variant_product_attr_unique');
        });

        Schema::table('productos_variantes_values', function (Blueprint $table) {
            $table->renameColumn('products_attribute_id', 'attribute_id');
        });

        Schema::table('productos_variantes_values', function (Blueprint $table) {
            $table->foreign('attribute_id')
                ->references('id')
                ->on('attribute')
                ->cascadeOnUpdate()
                ->cascadeOnDelete();

            $table->unique(['producto_variante_id', 'attribute_id'], 'prod_var_def_variant_attr_unique');
        });

        Schema::table('productos_variantes_values', function (Blueprint $table) {
            $table->dropIndex('prod_var_values_variant_id_index');
        });

        Schema::dropIfExists('products_attributes');
        Schema::rename('productos_variantes_values', 'productos_variantes_definiciones');
        Schema::rename('attribute', 'productos_attributes');
    }
};
