<?php

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

return new class extends Migration
{
    public function up(): void
    {
        Schema::table('stock', function (Blueprint $table) {
            // Adicionar coluna variant_id
            $table->foreignId('variant_id')
                  ->nullable()
                  ->after('product_id')
                  ->constrained('product_variations')
                  ->onDelete('cascade');
            
            // Adicionar índice
            $table->index('variant_id');
            
            // Remover unique key antiga e criar nova que inclui variant_id
            $table->dropUnique(['product_id', 'store_id']);
            $table->unique(['product_id', 'store_id', 'variant_id'], 'stock_product_store_variant_unique');
        });
    }

    public function down(): void
    {
        Schema::table('stock', function (Blueprint $table) {
            $table->dropForeign(['variant_id']);
            $table->dropColumn('variant_id');
            $table->dropIndex(['variant_id']);
            $table->dropUnique('stock_product_store_variant_unique');
            $table->unique(['product_id', 'store_id']);
        });
    }
};