<?php

namespace Database\Seeders;

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

class DatabaseSeeder extends Seeder
{
    /**
     * Seed the application's database.
     */
    public function run(): void
    {
        $this->call(AllTablesSeeder::class);

        $roles = [
            'super admin',
            'admin',
            'owner',
            'member',
        ];

        foreach ($roles as $role) {
            Role::firstOrCreate([
                'name' => $role,
                'guard_name' => 'web',
            ]);
        }

        $defaultUsers = [
            [
                'name' => 'Super Admin',
                'email' => 'superadmin@example.com',
                'role' => 'super admin',
            ],
            [
                'name' => 'Admin',
                'email' => 'admin@example.com',
                'role' => 'admin',
            ],
            [
                'name' => 'Subscription Owner',
                'email' => 'owner@example.com',
                'role' => 'owner',
            ],
            [
                'name' => 'Member User',
                'email' => 'member@example.com',
                'role' => 'member',
            ],
        ];

        foreach ($defaultUsers as $entry) {
            $user = User::updateOrCreate(
                ['email' => $entry['email']],
                [
                    'name' => $entry['name'],
                    'password' => Hash::make('12345678'),
                    'role' => $entry['role'] === 'super admin' ? 'admin' : $entry['role'],
                    'status' => 'active',
                ]
            );

            $user->syncRoles($entry['role']);
        }
    }
}
