<?php

use App\Models\User;
use Spatie\Permission\Models\Role;

// Check if user exists
$existingUser = User::where('email', 'admin@example.com')->first();

if ($existingUser) {
    echo "User admin@example.com already exists.\n";
    echo "User ID: " . $existingUser->id . "\n";
    echo "User Email: " . $existingUser->email . "\n";
    
    // Check if user has Super Admin role
    $superAdminRole = Role::where('name', 'Super Admin')->first();
    if ($superAdminRole && $existingUser->hasRole('Super Admin')) {
        echo "User already has Super Admin role.\n";
    } elseif ($superAdminRole) {
        $existingUser->assignRole($superAdminRole);
        echo "Super Admin role assigned successfully!\n";
    } else {
        echo "Super Admin role not found. Please run permission seeder first.\n";
    }
} else {
    echo "Creating new Super Admin user...\n";
    
    // Create new user
    $newUser = User::create([
        'first_name' => 'Super',
        'last_name' => 'Administrator', 
        'email' => 'admin@example.com',
        'password' => bcrypt('password123'),
        'email_verified_at' => now(),
    ]);
    
    // Assign Super Admin role
    $superAdminRole = Role::where('name', 'Super Admin')->first();
    if ($superAdminRole) {
        $newUser->assignRole($superAdminRole);
        echo "Super Admin user created and role assigned successfully!\n";
        echo "Email: admin@example.com\n";
        echo "Password: password123\n";
    } else {
        echo "User created but Super Admin role not found. Please run permission seeder first.\n";
    }
}