<?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('attivita', function (Blueprint $table) {
            $table->id();
            $table->string('nome');
            $table->string('descrizione')->nullable();
            $table->string('icona')->nullable();
            $table->string('colore')->nullable();
            $table->string('tipo')->nullable();
            $table->string('info')->nullable();
            $table->boolean('is_attiva')->default(true);
            $table->string('path_logo')->nullable();
            $table->string('path_icon')->nullable();
            $table->string('path_image')->nullable();
            $table->string('slug')->nullable();
            $table->timestamps();
        });
        Schema::table('ordine', function (Blueprint $table) {
            $table->foreign('attivita_id')->references('id')->on('attivita')->onDelete('cascade');
        });

        // Schema::table('attivita', function (Blueprint $table) {
        //     $table->foreign('attivita_id')->references('id')->on('attivita')->onDelete('cascade');
        // });


    // Aggiungi il campo attivita_id e la relativa FK a più tabelle
    $gruppoTabelle = ['dispositivo', 'cucina', 'prenotazione', 'evento', 'fornitore', 'piatto', 'bacheca', 'metodo_pagamento', 'categoria_contabile'];

    foreach ($gruppoTabelle as $nomeTabella) {
        Schema::table($nomeTabella, function (Blueprint $table) {
            if (!Schema::hasColumn($table->getTable(), 'attivita_id')) {
                $table->bigInteger('attivita_id')->unsigned()->nullable()->after('id');
            }
       
            $table->foreign('attivita_id')->references('id')->on('attivita')->onDelete('set null');
        });
    }
    }

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