<?php

namespace Database\Seeders;

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

class AdditionalProductionUsersSeeder extends Seeder
{
    /**
     * Seed additional fixed production users (idempotent / safe to re-run).
     */
    public function run(): void
    {
        $users = [
            ['email' => 'vanja@resovision.com',  'name' => 'Vanja',        'password' => 'Vanja123*#'],
            ['email' => 'tadeja@resovision.com', 'name' => 'Tadeja',       'password' => 'Tadeja123*#'],
            ['email' => 'sandi@resovision.com',  'name' => 'Sandi Kralj',  'password' => 'Sandi123*#'],
        ];

        foreach ($users as $data) {
            $user = User::query()->firstOrCreate(
                ['email' => $data['email']],
                [
                    'name' => $data['name'],
                    // Passwords will be hashed via the User model's casts.
                    'password' => $data['password'],
                ]
            );

            // If user already existed, update fields if needed
            if (! $user->wasRecentlyCreated) {
                $needsSave = false;

                if ($user->name !== $data['name']) {
                    $user->name = $data['name'];
                    $needsSave = true;
                }

                // Keep credentials in sync with expected defaults
                $user->password = $data['password'];
                $needsSave = true;

                if ($user->email_verified_at === null) {
                    $user->email_verified_at = now();
                    $needsSave = true;
                }

                if ($needsSave) {
                    $user->save();
                }
            } else {
                if ($user->email_verified_at === null) {
                    $user->email_verified_at = now();
                    $user->save();
                }
            }
        }
    }
}
