<?php

namespace Database\Seeders\Users;

use App\Models\User;
use Illuminate\Database\Seeder;

class UsersTableSeeder extends Seeder {
	public array $users = [

	];

	public array $admins = [
	    ['email' => 'shyctpuk@mail.ru', 'name' => 'Константин Митрофанов'],
		['email' => 'n.astashkevich@gmail.com', 'name' => 'Николай Асташкевич'],
		['email' => 'sergey@bodin.ru', 'name' => 'Сергей Бодин'],
		['email' => 'test@test.ru', 'name' => 'Иван Иванов']
	];


    /**
     * Run the database seeders.
     *
     * @return void
     */
    public function run() {
        $this->createUsers($this->users);
        $this->createUsers($this->admins, 'Administrator');
    }

    public function createUsers($users, $role = 'User') {
        collect($users)->each(function($data) use($role) {
            if ($email = $data['email'] ?? null) {
                if ($user = User::where(['email' => $email])->first()) $user->update($data);
                else $user = User::factory()->create($data);
                $user->assignRole($role);
            }
        });
	}
}
