<?php

namespace modules\coolify\config;

use classes\replication_secret_box;
use traits\module_config_variable;

class coolify_hetzner_cloud_api_token_c
{
    use module_config_variable {
        setVariableValue as private traitSetVariableValue;
    }

    public function __construct()
    {
        $this->setupConfigVariable(
            'Coolify',
            'hetzner_cloud_api_token',
            'string',
            false,
            null,
            'Hetzner Cloud API token with Load Balancer read/write permissions.',
            'pat_...',
            true,
            ''
        );
    }

    public function setVariableValue(mixed $value): void
    {
        $value = trim((string)($value ?? ''));
        if ($value !== '' && !str_starts_with($value, 'twsec:v1:')) {
            $value = replication_secret_box::encrypt($value);
        }

        $this->traitSetVariableValue($value);
    }
}
