<?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
    {
        // Check if table already exists to avoid conflicts
        if (!Schema::hasTable('stabili')) {
            Schema::create('stabili', function (Blueprint $table) {
                $table->id();
                $table->string('codice_stabile', 8)->unique();
                $table->string('denominazione');
                $table->string('indirizzo');
                $table->string('citta');
                $table->string('cap');
                $table->string('provincia');
                $table->string('nazione')->default('IT');
                $table->string('codice_fiscale')->nullable();
                $table->string('partita_iva')->nullable();
                $table->text('note')->nullable();
                $table->boolean('attivo')->default(true);
                $table->timestamps();
                $table->softDeletes();
            });
        } else {
            // Table already exists, just add any missing columns if needed
            Schema::table('stabili', function (Blueprint $table) {
                if (!Schema::hasColumn('stabili', 'codice_stabile')) {
                    $table->string('codice_stabile', 8)->unique()->after('id');
                }
                if (!Schema::hasColumn('stabili', 'attivo')) {
                    $table->boolean('attivo')->default(true)->after('note');
                }
            });
        }
    }

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