<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;
use App\Models\User;
use Illuminate\Support\Facades\Hash;
use Carbon\Carbon;
use Spatie\Permission\Models\Role;

class SuperAdminSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        // Verifica se il ruolo super-admin esiste, altrimenti lo crea
        $superAdminRole = Role::firstOrCreate(['name' => 'super-admin']);

        // Crea l'utente SuperAdmin se non esiste
        $superAdmin = User::firstOrCreate(
            ['email' => 'superadmin@example.com'],
            [
                'name' => 'Super Administrator',
                'password' => Hash::make('password'),
                'email_verified_at' => Carbon::now(),
            ]
        );

        // Assegna il ruolo super-admin all'utente
        if (!$superAdmin->hasRole('super-admin')) {
            $superAdmin->assignRole('super-admin');
        }

        $this->command->info('SuperAdmin user created/updated: superadmin@example.com / password');
    }
}
