<?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('documenti_stabili', function (Blueprint $table) {
            $table->id();
            $table->foreignId('stabile_id')->constrained('stabili')->onDelete('cascade');
            $table->string('nome_file');
            $table->string('nome_originale');
            $table->string('percorso_file');
            $table->string('categoria')->default('altri'); // contratti, amministrativi, tecnici, catastali, bancari, legali, assemblee, altri
            $table->string('tipo_mime');
            $table->bigInteger('dimensione'); // in bytes
            $table->text('descrizione')->nullable();
            $table->date('data_scadenza')->nullable(); // per contratti
            $table->string('tags')->nullable(); // tag separati da virgola
            $table->boolean('pubblico')->default(false); // visibile ai condomini
            $table->boolean('protetto')->default(false); // richiede password
            $table->string('password_hash')->nullable();
            $table->integer('versione')->default(1);
            $table->integer('downloads')->default(0);
            $table->timestamp('ultimo_accesso')->nullable();
            $table->foreignId('caricato_da')->nullable()->constrained('users');
            $table->timestamps();
            
            // Indici per performance
            $table->index(['stabile_id', 'categoria']);
            $table->index(['categoria', 'created_at']);
            $table->index('data_scadenza');
        });
    }

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