<?php

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

return new class extends Migration {
    public function up(): void
    {
        Schema::table('users', function (Blueprint $table) {
            $table->boolean('is_super_admin')->default(false)->after('role');
        });

        $hasSuperAdmin = DB::table('users')
            ->where('role', 'admin')
            ->where('is_super_admin', true)
            ->exists();

        if (!$hasSuperAdmin) {
            $firstAdminId = DB::table('users')
                ->where('role', 'admin')
                ->orderBy('id')
                ->value('id');

            if ($firstAdminId !== null) {
                DB::table('users')->where('role', 'admin')->update(['is_super_admin' => false]);
                DB::table('users')->where('id', $firstAdminId)->update(['is_super_admin' => true]);
            }
        }
    }

    public function down(): void
    {
        Schema::table('users', function (Blueprint $table) {
            $table->dropColumn('is_super_admin');
        });
    }
};
