<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    public function up(): void
    {
        Schema::create('item_requests', function (Blueprint $table) {
            $table->id();

            $table->foreignId('user_id')->constrained('users')->cascadeOnDelete();
            $table->foreignId('request_item_id')->constrained('request_items')->cascadeOnDelete();

            $table->decimal('quantity', 10, 2);
            $table->string('unit_snapshot', 50); // simpan satuan saat request dibuat
            $table->string('item_name_snapshot'); // simpan nama barang saat request dibuat
            $table->string('image_url_snapshot')->nullable();

            $table->date('request_date');
            $table->date('realization_date')->nullable();

            $table->text('purpose')->nullable(); // keperluan / keterangan user
            $table->text('admin_note')->nullable(); // catatan admin

            $table->enum('status', ['pending', 'approved', 'cancelled'])->default('pending');

            $table->foreignId('approved_by')->nullable()->constrained('users')->nullOnDelete();
            $table->timestamp('approved_at')->nullable();

            $table->timestamps();
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('item_requests');
    }
};
