<?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('piano_rateizzazione', function (Blueprint $table) {
            $table->id();
            $table->string('codice_piano')->unique();
            $table->foreignId('ripartizione_spese_id')->constrained('ripartizione_spese')->cascadeOnDelete();
            $table->foreignId('stabile_id')->constrained('stabili')->cascadeOnDelete();
            $table->string('descrizione');
            $table->string('tipo_piano')->default('standard'); // standard, personalizzato
            $table->decimal('importo_totale', 10, 2);
            $table->integer('numero_rate');
            $table->date('data_prima_rata');
            $table->string('frequenza')->default('mensile'); // mensile, bimestrale, trimestrale, semestrale
            $table->string('stato')->default('attivo'); // attivo, sospeso, completato, annullato
            $table->json('configurazione_rate')->nullable();
            $table->text('note')->nullable();
            $table->foreignId('creato_da')->constrained('users');
            $table->timestamp('attivato_at')->nullable();
            $table->foreignId('attivato_da')->nullable()->constrained('users');
            $table->timestamps();
            $table->softDeletes();
        });
    }

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