<?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('t_current_stocks', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('store_id');
            $table->unsignedBigInteger('raw_material_id');
            $table->decimal('qty_utuh', 10, 2)->default(0);
            $table->decimal('qty_pakai', 10, 2)->default(0);
            $table->decimal('qty_total_converted', 10, 2)->default(0); // Total in base UOM
            $table->string('uom_utuh', 10);
            $table->string('uom_pakai', 10);
            $table->decimal('conversion_factor', 8, 4)->default(1); // utuh to pakai conversion
            $table->date('last_updated_date');
            $table->timestamp('last_movement_time');
            $table->unsignedBigInteger('last_updated_by');
            $table->boolean('delete_flag')->default(false);
            $table->timestamps();

            $table->foreign('store_id')->references('id')->on('m_stores');
            $table->foreign('raw_material_id')->references('id')->on('m_raw_materials');
            $table->foreign('last_updated_by')->references('id')->on('users');
            
            $table->unique(['store_id', 'raw_material_id']);
            $table->index(['store_id', 'last_updated_date']);
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('t_current_stocks');
    }
};