<?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('invoice_items', function (Blueprint $table) {
            $table->foreignId('stock_batch_id')->nullable()->after('product_variant_id')->constrained('stock_batches')->nullOnDelete();
            $table->foreignId('product_serial_id')->nullable()->after('stock_batch_id')->constrained('product_serials')->nullOnDelete();
        });

        Schema::table('return_items', function (Blueprint $table) {
            $table->foreignId('stock_batch_id')->nullable()->after('product_variant_id')->constrained('stock_batches')->nullOnDelete();
            $table->foreignId('product_serial_id')->nullable()->after('stock_batch_id')->constrained('product_serials')->nullOnDelete();
        });

        Schema::table('damages', function (Blueprint $table) {
            $table->foreignId('stock_batch_id')->nullable()->after('product_variant_id')->constrained('stock_batches')->nullOnDelete();
            $table->foreignId('product_serial_id')->nullable()->after('stock_batch_id')->constrained('product_serials')->nullOnDelete();
        });
    }

    public function down(): void
    {
        Schema::table('invoice_items', function (Blueprint $table) {
            $table->dropConstrainedForeignId('product_serial_id');
            $table->dropConstrainedForeignId('stock_batch_id');
        });

        Schema::table('return_items', function (Blueprint $table) {
            $table->dropConstrainedForeignId('product_serial_id');
            $table->dropConstrainedForeignId('stock_batch_id');
        });

        Schema::table('damages', function (Blueprint $table) {
            $table->dropConstrainedForeignId('product_serial_id');
            $table->dropConstrainedForeignId('stock_batch_id');
        });
    }
};
