<?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
    {
        Schema::table('modules', function (Blueprint $table) {
            if (! Schema::hasColumn('modules', 'isparent')) {
                $table->boolean('isparent')->default(0)->after('name');
            }

            if (! Schema::hasColumn('modules', 'sorting')) {
                $table->integer('sorting')->default(0)->after('isparent');
            }

            if (! Schema::hasColumn('modules', 'icon')) {
                $table->string('icon')->nullable()->after('sorting');
            }

            if (! Schema::hasColumn('modules', 'isactive')) {
                $table->boolean('isactive')->default(1)->after('icon');
            }

            if (Schema::hasColumn('modules', 'description')) {
                $table->dropColumn('description');
            }
            if (Schema::hasColumn('modules', 'version')) {
                $table->dropColumn('version');
            }
            if (Schema::hasColumn('modules', 'is_active')) {
                $table->dropColumn('is_active');
            }
            if (Schema::hasColumn('modules', 'settings')) {
                $table->dropColumn('settings');
            }
        });
    }

    public function down(): void
    {
        Schema::table('modules', function (Blueprint $table) {
            if (Schema::hasColumn('modules', 'isparent')) {
                $table->dropColumn('isparent');
            }
            if (Schema::hasColumn('modules', 'sorting')) {
                $table->dropColumn('sorting');
            }
            if (Schema::hasColumn('modules', 'icon')) {
                $table->dropColumn('icon');
            }
            if (Schema::hasColumn('modules', 'isactive')) {
                $table->dropColumn('isactive');
            }

            if (! Schema::hasColumn('modules', 'description')) {
                $table->text('description')->nullable();
            }
            if (! Schema::hasColumn('modules', 'version')) {
                $table->string('version')->default('1.0.0');
            }
            if (! Schema::hasColumn('modules', 'is_active')) {
                $table->boolean('is_active')->default(true);
            }
            if (! Schema::hasColumn('modules', 'settings')) {
                $table->json('settings')->nullable();
            }
        });
    }
};

