<?php declare(strict_types=1);

namespace App\Entity;

use App\Controller\ConfigController;
use App\Controller\EncryptionController;
use Exception;
use OpenApi\Attributes as OAT;
use SodiumException;

/**
 *
 */
#[OAT\Schema(schema: 'nameserver')]

class Nameserver
{
    /**
     * @param string $name
     * @param int $id
     * @param string $a
     * @param string $aaaa
     * @param string $passphrase
     * @param string $apikey
     * @param string $apikeyPrefix
     */
    public function __construct(
        private string          $name,
        private int             $id = 0,
        private string          $a = '',
        private string          $aaaa = '',
        private readonly string $passphrase = '',
        private string          $apikey = '',
        private string          $apikeyPrefix = '')
	{
        if ($this->passphrase) {
            $configController = new ConfigController(quiet: false);
            $encryptionController = new EncryptionController();

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

            [$this->apikeyPrefix] = explode(separator: '.', string: $this->passphrase);

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

    }

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

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


	/**
	 * @return string
	 */
	#[OAT\Property(type: 'string')]
	public function getA(): string
	{
		return $this->a;
	}

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

	/**
	 * @return string
	 */
	#[OAT\Property(type: 'string')]
	public function getAaaa(): string
	{
		return $this->aaaa;
	}

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

	/**
	 * @return string
	 */
	#[OAT\Property(type: 'string')]
	public function getApikey(): string
	{
		return $this->apikey;
	}

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

	/**
	 * @return int
	 */
	#[OAT\Property(type: 'int')]
	public function getId(): int
	{
		return $this->id;
	}

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

	/**
	 * @return string
	 */
	#[OAT\Property(type: 'string')]
	public function getName(): string
	{
		return $this->name;
	}

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

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

}