<?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']);
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('products_attributes');
        Schema::rename('productos_variantes_values', 'productos_variantes_definiciones');
        Schema::rename('attribute', 'productos_attributes');
    }
};
