<?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('movimenti_chiavi', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('chiave_id');
            $table->enum('tipo_movimento', ['assegnazione', 'riconsegna', 'smarrimento', 'sostituzione']);
            $table->timestamp('data_movimento')->useCurrent();
            $table->string('assegnata_da')->nullable()->comment('Chi ha fatto l\'assegnazione');
            $table->string('assegnata_a')->nullable()->comment('A chi è stata assegnata');
            $table->string('motivo')->nullable();
            $table->text('note')->nullable();
            $table->timestamps();
            
            // Foreign keys e indici
            $table->foreign('chiave_id')->references('id')->on('chiavi_stabili')->onDelete('cascade');
            $table->index(['chiave_id', 'data_movimento'], 'idx_chiave_data');
        });
    }

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