<?php

namespace Database\Seeders;

use App\Enums\UserRole;
use App\Models\User;
use Illuminate\Database\Seeder;
use Illuminate\Support\Str;

class ProjectUsersSeeder extends Seeder
{
    public function run(): void
    {
        $users = [
            [
                'name' => 'Ricardo Medeiros',
                'email' => 'ricardo.medeiros@katoennatie.com',
                'role' => UserRole::Admin,
            ],
            [
                'name' => 'David Gomes',
                'email' => 'david.gomes@katoennatie.com.br',
                'role' => UserRole::Collaborator,
            ],
            [
                'name' => 'Reginaldo Rodrigues',
                'email' => 'reginaldo.rodrigues@katoennatie.com',
                'role' => UserRole::Collaborator,
            ],
            [
                'name' => 'Marcelo Martins',
                'email' => 'marcelo.martins@katoennatie.com',
                'role' => UserRole::Viewer,
            ],
        ];

        foreach ($users as $attributes) {
            $user = User::query()
                ->where('email', $attributes['email'])
                ->first()
                ?? User::query()->where('name', $attributes['name'])->first()
                ?? new User;

            $user->forceFill([
                ...$attributes,
                'is_active' => true,
                'email_verified_at' => $user->email_verified_at ?? now(),
            ]);

            if (! $user->exists) {
                $user->password = Str::password(40);
            }

            $user->save();
        }
    }
}
