forgetCachedPermissions(); // 1. Delete all users except superadmin@gsb-cons.com $superAdmin = User::where('email', 'superadmin@gsb-cons.com')->first(); if ($superAdmin) { User::where('id', '!=', $superAdmin->id)->delete(); $this->command->info("Deleted all non-superadmin users."); } else { User::query()->delete(); $superAdmin = User::create([ 'name' => 'Super Administrator', 'email' => 'superadmin@gsb-cons.com', 'password' => bcrypt('password'), 'user_type' => 'admin', 'status' => 'active', 'contractor_id' => null, ]); $this->command->info("Created fresh Super Administrator account."); } // 2. Ensure Super Admin role & permissions $superAdminRole = Role::firstOrCreate(['name' => 'Super Admin', 'guard_name' => 'web']); $adminRole = Role::firstOrCreate(['name' => 'admin', 'guard_name' => 'web']); $pmRole = Role::firstOrCreate(['name' => 'Project Manager', 'guard_name' => 'web']); $contractorRole = Role::firstOrCreate(['name' => 'Main Contractor Admin', 'guard_name' => 'web']); $supervisorRole = Role::firstOrCreate(['name' => 'Construction Supervisor', 'guard_name' => 'web']); $siteTechRole = Role::firstOrCreate(['name' => 'Site Technical', 'guard_name' => 'web']); $allPermissions = Permission::all(); $superAdminRole->syncPermissions($allPermissions); $adminRole->syncPermissions($allPermissions); // Guarantee super admin state $superAdmin->update([ 'user_type' => 'admin', 'status' => 'active', 'contractor_id' => null, ]); $superAdmin->syncRoles(['Super Admin', 'admin']); // Profile for Super Admin $profile = EmployeeProfile::firstOrCreate( ['user_id' => $superAdmin->id], [ 'department' => 'Administration', 'position' => 'Super Administrator', 'employee_code' => 'SUPER-0001', 'hire_date' => now(), ] ); $superAdmin->update([ 'userable_type' => EmployeeProfile::class, 'userable_id' => $profile->id, ]); app()[PermissionRegistrar::class]->forgetCachedPermissions(); $this->command->info("Database user cleanup complete. Retained only Super Administrator (superadmin@gsb-cons.com)."); } }