<?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('parts', function (Blueprint $table) {
            // Update existing supplier column to foreign key
            $table->dropColumn('supplier');
            $table->foreignId('supplier_id')->nullable()->after('location')->constrained()->onDelete('set null');
            
            // Add new inventory fields
            $table->string('barcode')->nullable()->after('status');
            $table->decimal('weight', 8, 2)->nullable()->after('barcode');
            $table->string('dimensions')->nullable()->after('weight');
            $table->integer('warranty_period')->nullable()->after('dimensions'); // in days
            $table->string('image')->nullable()->after('warranty_period');
            
            // Add indexes for better performance
            $table->index(['category', 'status']);
            $table->index('barcode');
            $table->index(['quantity_on_hand', 'minimum_stock_level']);
        });
    }

    public function down(): void
    {
        Schema::table('parts', function (Blueprint $table) {
            $table->dropForeign(['supplier_id']);
            $table->dropColumn([
                'supplier_id',
                'barcode',
                'weight',
                'dimensions',
                'warranty_period',
                'image'
            ]);
            $table->string('supplier')->nullable();
            
            $table->dropIndex(['category', 'status']);
            $table->dropIndex(['barcode']);
            $table->dropIndex(['quantity_on_hand', 'minimum_stock_level']);
        });
    }
};
