<?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::create('stock_adjustments', function (Blueprint $table) {
            $table->id();
            $table->foreignId('product_id')->constrained('products')->onDelete('cascade');
            $table->unsignedBigInteger('variant_id')->nullable();
            $table->enum('adjustment_type', ['addition', 'subtraction']);
            $table->decimal('before_qty', 10, 2);
            $table->decimal('adjusted_qty', 10, 2);
            $table->decimal('after_qty', 10, 2);
            $table->string('reason');
            $table->text('remarks');
            $table->enum('reference_type', ['audit', 'manual']);
            $table->unsignedBigInteger('reference_id')->nullable();
            $table->foreignId('created_by')->constrained('users')->onDelete('cascade');
            $table->timestamps();

            $table->foreign('variant_id')->references('id')->on('products')->onDelete('cascade');
            $table->index('product_id');
            $table->index('variant_id');
            $table->index('reference_type');
            $table->index('reference_id');
            $table->index('created_at');
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('stock_adjustments');
    }
};
