<?php

use App\Enums\ProductStockQuality;
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
    {
        Schema::table('stok_opname_items', function (Blueprint $table) {
            $table->dropForeign(['stok_opname_id']);
            $table->dropForeign(['product_variant_id']);
            $table->dropUnique('stok_opname_items_stok_opname_id_product_variant_id_unique');
            
            $table->enum('stock_quality', ProductStockQuality::values())->default(ProductStockQuality::GOOD->value)->after('product_variant_id');
            $table->unique(['stok_opname_id', 'product_variant_id', 'stock_quality'], 'stok_opname_items_opname_variant_quality_unique');
            
            $table->foreign('stok_opname_id')->references('id')->on('stok_opnames')->cascadeOnDelete();
            $table->foreign('product_variant_id')->references('id')->on('product_variants')->cascadeOnDelete();
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::table('stok_opname_items', function (Blueprint $table) {
            $table->dropForeign(['stok_opname_id']);
            $table->dropForeign(['product_variant_id']);
            $table->dropUnique('stok_opname_items_opname_variant_quality_unique');
            
            $table->dropColumn('stock_quality');
            $table->unique(['stok_opname_id', 'product_variant_id']);
            
            $table->foreign('stok_opname_id')->references('id')->on('stok_opnames')->cascadeOnDelete();
            $table->foreign('product_variant_id')->references('id')->on('product_variants')->cascadeOnDelete();
        });
    }
};
