<?php

namespace Database\Seeders;

use App\Models\Permission;
use App\Models\Role;
use App\Models\User;
use App\Services\WalletService;
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;

class DatabaseSeeder extends Seeder
{
    use WithoutModelEvents;

    /**
     * Seed the application's database.
     */
    public function run(): void
    {
        // User::factory(10)->create();

        $permissions = collect([
            ['name' => 'دسترسی به پنل مدیریت', 'slug' => 'admin.access'],
            ['name' => 'مدیریت کاربران', 'slug' => 'users.manage'],
            ['name' => 'مدیریت نقش‌ها', 'slug' => 'roles.manage'],
            ['name' => 'مدیریت دسترسی‌ها', 'slug' => 'permissions.manage'],
            ['name' => 'مدیریت کیف پول', 'slug' => 'wallets.manage'],
        ])->map(fn (array $permission) => Permission::query()->updateOrCreate(
            ['slug' => $permission['slug']],
            ['name' => $permission['name']],
        ));

        $adminRole = Role::query()->updateOrCreate(
            ['slug' => 'admin'],
            ['name' => 'مدیر'],
        );

        $adminRole->permissions()->sync($permissions->pluck('id'));

        $user = User::query()->firstOrCreate(
            ['email' => 'test@example.com'],
            [
                'name' => 'Test User',
                'mobile' => '09120000000',
                'email_verified_at' => now(),
                'password' => 'password',
            ],
        );

        $user->roles()->sync([$adminRole->id]);

        User::query()->each(fn (User $user) => app(WalletService::class)->createForUser($user));
    }
}
