<?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('stock_outs', function (Blueprint $table) {
            $table->boolean('is_return')->default(false)->after('notes');
            $table->foreignId('original_stock_out_id')->nullable()->after('is_return')->constrained('stock_outs')->nullOnDelete();
            $table->text('return_reason')->nullable()->after('original_stock_out_id');
            $table->foreignId('returned_by')->nullable()->after('return_reason')->constrained('users')->nullOnDelete();
            
            // Indexes
            $table->index('is_return');
            $table->index('original_stock_out_id');
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::table('stock_outs', function (Blueprint $table) {
            $table->dropForeign(['original_stock_out_id']);
            $table->dropForeign(['returned_by']);
            $table->dropIndex(['is_return']);
            $table->dropIndex(['original_stock_out_id']);
            $table->dropColumn(['is_return', 'original_stock_out_id', 'return_reason', 'returned_by']);
        });
    }
};
