<?php declare(strict_types=1);

namespace App\Entity;

use App\Controller\ConfigController;
use App\Controller\EncryptionController;
use Exception;
use SodiumException;

/**
 *
 */
class Apikey
{

    public function __construct(
        private int             $id = 0,
        private string          $name = '',
        private string          $apikey = '',
        private string          $apikeyPrefix = '',
        private readonly string $passphrase = ''
    )
    {
        if ($this->passphrase) {
            $configController = new ConfigController(quiet: true);
            $encryptionController = new EncryptionController();

            $encryptionKey = $configController->getConfig(configKey: 'encryptionKey');

            $this->apikey = strtok(string: $this->passphrase, token: '.');

            try {
                $this->apikey = $encryptionController->safeEncrypt(message: $this->passphrase, key: $encryptionKey);
            } catch (Exception|SodiumException $e) {
                exit($e->getMessage() . PHP_EOL);
            }
        }
    }

    /**
     * @return string
     */
    public function getPassphrase(): string
    {
        return $this->passphrase;
    }


    /**
     * @return String
     */
    public function getApikey(): string
    {
        return $this->apikey;
    }

    /**
     * @return string
     */
    public function getApikeyPrefix(): string
    {
        return $this->apikeyPrefix;
    }


    /**
     * @return int
     */
    public function getId(): int
    {
        return $this->id;
    }

    /**
     * @param int $id
     */
    public function setId(int $id): void
    {
        $this->id = $id;
    }

    /**
     * @return String
     */
    public function getName(): string
    {
        return $this->name;
    }

    /**
     * @param string $apikeyPrefix
     */
    public function setApikeyPrefix(string $apikeyPrefix): void
    {
        $this->apikeyPrefix = $apikeyPrefix;
    }

    /**
     * @param String $apiToken
     */
    public function setApikey(string $apikey): void
    {
        $this->apikey = $apikey;
    }


    /**
     * @param String $name
     */
    public function setName(string $name): void
    {
        $this->name = $name;
    }

}