<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    public function up(): void
    {
        if (! Schema::hasTable('store_groups')) {
            Schema::create('store_groups', function (Blueprint $table) {
                $table->id();
                $table->string('name');
                $table->boolean('is_active')->default(true);
                $table->timestamps();
            });

            return;
        }

        Schema::table('store_groups', function (Blueprint $table) {
            if (! Schema::hasColumn('store_groups', 'name')) {
                $table->string('name')->after('id');
            }

            if (! Schema::hasColumn('store_groups', 'is_active')) {
                $table->boolean('is_active')->default(true)->after('name');
            }

            if (! Schema::hasColumn('store_groups', 'created_at')) {
                $table->timestamps();
            }
        });
    }

    public function down(): void
    {
        // Intencionalmente no hacemos drop para evitar pérdida de datos en entornos existentes.
    }
};
