<?php

namespace modules\coolify;

require_once WD . '/modules/coolify/config/coolify_enabled_c.php';
require_once WD . '/modules/coolify/config/coolify_hetzner_cloud_api_token_c.php';
require_once WD . '/modules/coolify/config/coolify_hetzner_load_balancer_id_c.php';
require_once WD . '/modules/coolify/config/coolify_lb_automation_enabled_c.php';
require_once WD . '/modules/coolify/config/coolify_lb_automation_mode_c.php';
require_once WD . '/modules/coolify/config/coolify_public_gateway_host_c.php';

use modules\coolify\config\coolify_hetzner_cloud_api_token_c;
use modules\coolify\config\coolify_hetzner_load_balancer_id_c;
use modules\coolify\config\coolify_enabled_c;
use modules\coolify\config\coolify_lb_automation_enabled_c;
use modules\coolify\config\coolify_lb_automation_mode_c;
use modules\coolify\config\coolify_public_gateway_host_c;
use traits\module_config_t;

class coolify_c
{
    use module_config_t {
        getConfigRequest as private traitGetConfigRequest;
    }

    public coolify_enabled_c $enabled;
    public coolify_lb_automation_enabled_c $lb_automation_enabled;
    public coolify_lb_automation_mode_c $lb_automation_mode;
    public coolify_hetzner_load_balancer_id_c $hetzner_load_balancer_id;
    public coolify_hetzner_cloud_api_token_c $hetzner_cloud_api_token;
    public coolify_public_gateway_host_c $public_gateway_host;

    public function __construct()
    {
        $this->setupConfig('Coolify');
        $this->allowUpdate([
            coolify_enabled_c::class,
            coolify_lb_automation_enabled_c::class,
            coolify_lb_automation_mode_c::class,
            coolify_hetzner_load_balancer_id_c::class,
            coolify_hetzner_cloud_api_token_c::class,
            coolify_public_gateway_host_c::class,
        ]);
        $this->enabled = new coolify_enabled_c();
        $this->lb_automation_enabled = new coolify_lb_automation_enabled_c();
        $this->lb_automation_mode = new coolify_lb_automation_mode_c();
        $this->hetzner_load_balancer_id = new coolify_hetzner_load_balancer_id_c();
        $this->hetzner_cloud_api_token = new coolify_hetzner_cloud_api_token_c();
        $this->public_gateway_host = new coolify_public_gateway_host_c();
    }

    public function getConfigRequest(): array
    {
        return array_map(static function (array $row): array {
            if (($row['variable'] ?? '') === 'hetzner_cloud_api_token') {
                $secretSet = trim((string)($row['value'] ?? '')) !== '';
                $row['value'] = $secretSet ? '[redacted]' : '';
                $row['secret_set'] = $secretSet;
            }
            return $row;
        }, $this->traitGetConfigRequest());
    }
}
