<?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('indirect_goods_requests', function (Blueprint $table) {
            $table->id();
            $table->string('request_number')->unique();
            $table->date('request_date');
            $table->foreignId('project_id')
                  ->constrained('projects')->onDelete('cascade');
            $table->foreignId('division_id')->nullable()
                  ->constrained('project_divisions')->onDelete('set null');

            // Workflow
            $table->enum('status', [
                'draft', 'pending_approval', 'approved', 'rejected',
                'issued', 'partially_issued', 'cancelled'
            ])->default('draft');
            $table->boolean('requires_approval')->default(true);

            // People
            $table->foreignId('requested_by')
                  ->constrained('users')->onDelete('cascade');
            $table->foreignId('approved_by')->nullable()
                  ->constrained('users')->onDelete('set null');
            $table->dateTime('approved_at')->nullable();
            $table->foreignId('issued_by')->nullable()
                  ->constrained('users')->onDelete('set null');
            $table->dateTime('issued_at')->nullable();

            $table->string('receiver_name')->nullable();
            $table->text('notes')->nullable();
            $table->text('rejection_reason')->nullable();
            $table->string('request_file')->nullable();

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

            $table->index(['status', 'request_date']);
            $table->index('project_id');
        });
    }

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