<?php

namespace Database\Seeders;

use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;
use App\Models\User;
use Illuminate\Support\Facades\DB;
use Spatie\Permission\Models\Role;

class AdminSeeder extends Seeder
{
    /**
     * Run the database seeds.
     */
    public function run(): void
    {

        // Retrieve the maximum id from the bookings table
        $maxId = DB::table('pricing_plans')->max('id');
        // Set the sequence to the maximum id + 1
        DB::statement("SELECT setval('pricing_plans_id_seq', ?, false)", [$maxId + 1]);

 
        dd(bcrypt('Hello@1985'));
        $adminRole = Role::where('name', 'Admin')->first();

        if (!$adminRole) {
            $this->command->error('Role "Admin" not found. Please create the role first.');
            return;
        }

        // Create Admin User
        $admin = User::updateOrCreate(
            ['email' => 'admin@Luqia.com'], // Ensure idempotency
            [
                'name' => 'Admin User',
                'password' => bcrypt('P@55worD123'), // Use a secure password
            ]
        );

        // Assign the existing Admin Role
        $admin->assignRole($adminRole);

        // Output success message
        $this->command->info('Admin user created and role assigned successfully!');
    }
}
