<?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('inventories', function (Blueprint $table) {
            $table->enum('transaction_type', ['in', 'out'])->default('in')->after('last_transaction_at');
            $table->string('reference_type')->nullable()->after('transaction_type'); // 'addition', 'transfer', 'return', 'adjustment', 'supplier_bill', etc.
            $table->unsignedBigInteger('reference_id')->nullable()->after('reference_type'); // ID of related record (stock_outs, supplier_bills, etc.)
            
            // Add indexes for performance
            $table->index('transaction_type');
            $table->index(['reference_type', 'reference_id']);
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::table('inventories', function (Blueprint $table) {
            $table->dropIndex(['reference_type', 'reference_id']);
            $table->dropIndex(['transaction_type']);
            $table->dropColumn(['transaction_type', 'reference_type', 'reference_id']);
        });
    }
};
