<?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('order_reviews', function (Blueprint $table) {
            $table->foreignId('product_id')->nullable()->after('customer_id')->constrained()->nullOnDelete();
            $table->unique(['order_id', 'product_id']);
            $table->dropUnique(['order_id']);
        });

        Schema::table('order_complaints', function (Blueprint $table) {
            $table->foreignId('product_id')->nullable()->after('customer_id')->constrained()->nullOnDelete();
            $table->unique(['order_id', 'product_id']);
            $table->dropUnique(['order_id']);
        });
    }

    public function down(): void
    {
        Schema::table('order_reviews', function (Blueprint $table) {
            $table->unique('order_id');
            $table->dropUnique(['order_id', 'product_id']);
            $table->dropConstrainedForeignId('product_id');
        });

        Schema::table('order_complaints', function (Blueprint $table) {
            $table->unique('order_id');
            $table->dropUnique(['order_id', 'product_id']);
            $table->dropConstrainedForeignId('product_id');
        });
    }
};
