<?php

namespace Database\Seeders;

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

class AdminUserSeeder extends Seeder
{
    public function run(): void
    {
        // Pastikan role sudah ada sebelum assign
        // (RolePermissionSeeder harus dijalankan lebih dulu)

        // ── Super Admin ────────────────────────────────────────────────────
        $superAdmin = User::updateOrCreate(
            ['email' => 'superadmin@titipinbabe.com'],
            [
                'name'      => 'Super Admin',
                'phone'     => '081111111111',
                'password'  => Hash::make('password'),
                'is_active' => true,
            ]
        );
        // syncRoles menggunakan guard 'web' secara default (dari config/auth.php)
        $superAdmin->syncRoles(['super_admin']);

        // ── Admin Operasional ──────────────────────────────────────────────
        $admin = User::updateOrCreate(
            ['email' => 'admin@titipinbabe.com'],
            [
                'name'      => 'Admin Operasional',
                'phone'     => '082222222222',
                'password'  => Hash::make('password'),
                'is_active' => true,
            ]
        );
        $admin->syncRoles(['admin']);

        $this->command->info('');
        $this->command->info('✓ Akun admin berhasil dibuat:');
        $this->command->table(
            ['Role', 'Email', 'Password'],
            [
                ['super_admin', 'superadmin@titipinbabe.com', 'password'],
                ['admin',       'admin@titipinbabe.com',      'password'],
            ]
        );

        // Verifikasi
        $this->command->info('');
        $this->command->info('Verifikasi roles:');
        $this->command->info('  superadmin: ' . $superAdmin->fresh()->getRoleNames()->implode(', '));
        $this->command->info('  admin: ' . $admin->fresh()->getRoleNames()->implode(', '));
    }
}
