<?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('accounts', function (Blueprint $table) {
            // Add Item-specific fields for when model_type = 'Item'
            $table->string('sku')->nullable()->after('description');
            $table->enum('type', ['RM', 'FG'])->nullable()->after('sku');
            $table->string('barcode')->nullable()->after('type');
            $table->decimal('reorder_level', 10, 6)->nullable()->after('barcode');
            $table->enum('status', ['active', 'inactive', 'draft', 'discontinued'])->default('active')->after('reorder_level');
            $table->string('unit_of_measure')->nullable()->after('status');

            // Add indexes for performance
            $table->index('sku');
            $table->index('type');
            $table->index('status');
            $table->unique(['sku'], 'accounts_sku_unique');
            $table->unique(['barcode'], 'accounts_barcode_unique');
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::table('accounts', function (Blueprint $table) {
            $table->dropUnique('accounts_sku_unique');
            $table->dropUnique('accounts_barcode_unique');
            $table->dropIndex(['sku', 'type', 'status']);
            $table->dropColumn(['sku', 'type', 'barcode', 'reorder_level', 'status', 'unit_of_measure']);
        });
    }
};
