<?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('fondi_condominiali', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('stabile_id');
            $table->enum('tipo_fondo', [
                'ordinario',
                'riserva',
                'ascensore',
                'riscaldamento',
                'facciata_tetto',
                'verde_giardini',
                'sicurezza',
                'innovazione',
                'investimenti',
                'personalizzato'
            ]);
            $table->string('denominazione');
            $table->text('descrizione')->nullable();
            $table->decimal('saldo_attuale', 12, 2)->default(0.00);
            $table->decimal('saldo_minimo', 12, 2)->default(0.00);
            $table->decimal('percentuale_accantonamento', 5, 2)->default(0.00);
            $table->boolean('attivo')->default(true);
            $table->timestamps();
            
            // Foreign keys e indici
            $table->foreign('stabile_id')->references('id')->on('stabili')->onDelete('cascade');
            $table->index(['stabile_id', 'tipo_fondo'], 'idx_stabile_tipo_fondo');
        });
    }

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