<?php

namespace Database\Seeders;

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

class ProductionUserSeeder extends Seeder
{
    /**
     * Seed fixed production users (idempotent / safe to re-run).
     */
    public function run(): void
    {
        $users = [
            ['email' => 'klara@resovision.com',   'name' => 'Klara Koren',        'password' => 'Klara123*#'],
            ['email' => 'beno@resovision.com',    'name' => 'Benjamin Gašparič',  'password' => 'Beno123*#'],
            ['email' => 'srecko@resovision.com',  'name' => 'Srečko Lazer',       'password' => 'Srecko123*#'],
            ['email' => 'tamara@resovision.com',  'name' => 'Tamara Koren',       'password' => 'Tamara123*#'],
        ];

        foreach ($users as $data) {
            $user = User::query()->firstOrCreate(
                ['email' => $data['email']],
                [
                    'name' => $data['name'],
                    'password' => $data['password'], // hashed via cast
                ]
            );

            // If user already existed, update name/password if changed
            if (! $user->wasRecentlyCreated) {
                $needsSave = false;
                if ($user->name !== $data['name']) {
                    $user->name = $data['name'];
                    $needsSave = true;
                }
                // Always re-set password to ensure known credential (optional: skip if security requires manual reset)
                $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();
                }
            }
        }
    }
}
