<?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('assegnazioni', function (Blueprint $table) {
            $table->id();
            $table->foreignId('territorio_id')->constrained('territori')->restrictOnDelete();
            $table->foreignId('proclamatore_id')->constrained('proclamatori')->restrictOnDelete();
            $table->foreignId('anno_teocratico_id')->constrained('anni_teocratici')->restrictOnDelete();
            $table->date('assigned_at');
            $table->date('returned_at')->nullable();
            $table->boolean('counted_in_campaign')->nullable();
            $table->foreignId('campaign_id')->nullable()->constrained('campagne')->nullOnDelete();
            $table->text('note')->nullable();
            $table->foreignId('created_by')->constrained('users')->restrictOnDelete();
            $table->foreignId('returned_by')->nullable()->constrained('users')->nullOnDelete();
            $table->timestamps();

            // Composite indexes for performance
            $table->index(['territorio_id', 'returned_at']);
            $table->index(['proclamatore_id', 'returned_at']);
            $table->index(['anno_teocratico_id']);
        });
    }

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