<?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
    {
        Schema::table('return_requests', function (Blueprint $table) {
            $table->foreignId('exchange_order_id')->nullable()->constrained('orders')->nullOnDelete()->after('refund_method');
            $table->decimal('exchange_shipping_cost', 10, 2)->default(0.00)->after('exchange_order_id');
        });

        Schema::table('orders', function (Blueprint $table) {
            $table->boolean('is_exchange')->default(false)->after('status');
            $table->foreignId('parent_order_id')->nullable()->constrained('orders')->nullOnDelete()->after('is_exchange');
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::table('orders', function (Blueprint $table) {
            $table->dropForeign(['parent_order_id']);
            $table->dropColumn(['is_exchange', 'parent_order_id']);
        });

        Schema::table('return_requests', function (Blueprint $table) {
            $table->dropForeign(['exchange_order_id']);
            $table->dropColumn(['exchange_order_id', 'exchange_shipping_cost']);
        });
    }
};
