<?php

declare(strict_types=1);

namespace PhpWA\Auth;

use PhpWA\Crypto\PreKeyFactory;
use PhpWA\Crypto\RandomSourceInterface;
use PhpWA\Crypto\SodiumCompatXEdDsa;
use PhpWA\Crypto\SystemRandomSource;
use PhpWA\Crypto\X25519;

final class CredentialsFactory
{
    /** @var RandomSourceInterface */ private $random;
    /** @var PreKeyFactory */ private $preKeys;

    public function __construct(?RandomSourceInterface $random = null)
    {
        $this->random = $random ?? new SystemRandomSource();
        $this->preKeys = new PreKeyFactory($this->random, new SodiumCompatXEdDsa($this->random));
    }

    public function create(): AuthenticationCredentials
    {
        $identity = X25519::generate($this->random);
        return new AuthenticationCredentials(
            X25519::generate($this->random),
            X25519::generate($this->random),
            $identity,
            $this->preKeys->signedPreKey($identity, 1),
            $this->preKeys->registrationId(),
            $this->random->bytes(32)
        );
    }
}
