<?php

namespace App\Repositories;

use App\Models\User;
use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Str;
use Ramsey\Uuid\Uuid;

class UserRepository
{
    /**
     * New guest user
     */
    public static function guestUser(): User
    {
        // Generate a version 4 (random) UUID
        return User::create([
            'first_name' => 'Guest',
            'last_name' => 'User',
            'email' => sprintf('guest.%s@shop.post.tm', Uuid::uuid4()->toString()),
            'password' => Str::random(6),
        ]);
    }

    /**
     * Register new user
     */
    public static function registerUser(Request $request): Closure
    {
        return fn () => User::create([
            'phone_number' => $request->phone_number,
            'first_name' => $request->first_name ?: 'Registered',
            'last_name' => $request->last_name ?: 'User',
            'email' => $request->email ?: sprintf('registered.%s@shop.post.tm', random_int(10000000, 99999999)),
            'password' => Str::random(6),
            'options' => $request->address ? sprintf('{"address":"%s"}', $request->address) : null,
        ]);
    }
}
