<?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('supplier_documents', function (Blueprint $table) {
            $table->foreignId('supplier_id')->nullable()->after('id')->constrained('suppliers')->nullOnDelete();
        });

        Schema::table('grns', function (Blueprint $table) {
            $table->foreignId('supplier_id')->nullable()->after('supplier_document_id')->constrained('suppliers')->nullOnDelete();
            $table->foreignId('purchase_order_id')->nullable()->after('supplier_id')->constrained('purchase_orders')->nullOnDelete();
        });
    }

    public function down(): void
    {
        Schema::table('grns', function (Blueprint $table) {
            $table->dropConstrainedForeignId('purchase_order_id');
            $table->dropConstrainedForeignId('supplier_id');
        });

        Schema::table('supplier_documents', function (Blueprint $table) {
            $table->dropConstrainedForeignId('supplier_id');
        });
    }
};
