<?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('ripartizione_spese', function (Blueprint $table) {
            $table->id();
            $table->string('codice_ripartizione')->unique();
            $table->foreignId('voce_spesa_id')->constrained('voci_spesa')->cascadeOnDelete();
            $table->foreignId('stabile_id')->constrained('stabili')->cascadeOnDelete();
            $table->unsignedBigInteger('tabella_millesimale_id');
            $table->decimal('importo_totale', 10, 2);
            $table->decimal('importo_ripartito', 10, 2)->default(0);
            $table->string('tipo_ripartizione')->default('millesimale'); // millesimale, uguale, personalizzata
            $table->date('data_ripartizione');
            $table->string('stato')->default('bozza'); // bozza, approvata, contabilizzata
            $table->text('note')->nullable();
            $table->json('configurazione_ripartizione')->nullable();
            $table->foreignId('creato_da')->constrained('users');
            $table->timestamp('approvato_at')->nullable();
            $table->unsignedBigInteger('approvato_da')->nullable();
            $table->timestamp('contabilizzato_at')->nullable();
            $table->unsignedBigInteger('contabilizzato_da')->nullable();
            $table->timestamps();
            $table->softDeletes();
        });
    }

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