<?php

class SettingsHelper
{
    public const PASSWORD_COMPLEXITY = 'passwordComplexity';
    public const MIN_PASSWORD_LENGTH = 'minPasswordLength';
    public const PASSWORD_EXPIRATION_AFTER_RESET_HOURS = 'passwordExpirationAfterResetHours';
    public const ENFORCE_2FA = 'enforce2FA';
    public const ALLOW_LLM_VIEW_ACCESS_TO_ATTRIBUTES_NAMES = 'allowLlmViewAccessToAttributesNames';
    public const DISABLE_AI = 'disableAI';

    public const INTEGERS = [
        self::PASSWORD_COMPLEXITY,
        self::MIN_PASSWORD_LENGTH,
        self::PASSWORD_EXPIRATION_AFTER_RESET_HOURS
    ];

    public const BOOLEANS = [
        self::ENFORCE_2FA,
        self::ALLOW_LLM_VIEW_ACCESS_TO_ATTRIBUTES_NAMES,
        self::DISABLE_AI,
    ];

    public const DEFAULTS = [
        self::PASSWORD_COMPLEXITY => 3,
        self::MIN_PASSWORD_LENGTH => 8,
        self::PASSWORD_EXPIRATION_AFTER_RESET_HOURS => 48
    ];

    /** @var array */
    protected static $allowedSettings = [
        'rbacEnabled',
        'ldapEnabled',
        'hostIdentifier',
        'blueHostHorizon',
        'hostsCollisionsThreshold',
        'passwordComplexity',
        'minPasswordLength',
        'passwordExpirationAfterResetHours',
        'enforce2FA',
        'logLevel',
        'ldap_enable_role_syncing',
        'ldap_roles_list_to_sync',
        'ldap_perform_sync_on_login',
        'ldap_remove_role_on_syncing',
        self::ALLOW_LLM_VIEW_ACCESS_TO_ATTRIBUTES_NAMES,
        self::DISABLE_AI
    ];

    /**
     * Remove LDAP Password from settings
     *
     * @param $settings
     * @return string
     */
    public static function removeLdapPassword($settings)
    {
        if (is_string($settings)) {
            $settings = json_decode($settings);
            if (isset($settings->data) && isset($settings->data[0]) && isset($settings->data[0]->ldapPassword)) {
                unset($settings->data[0]->ldapPassword);
            }
        }
        return json_encode($settings, JSON_PRETTY_PRINT);
    }

    /**
     * Remove not allowed settings
     *
     * @param $data
     */
    public static function removeUnAllowedSettings(&$data)
    {
        foreach ($data as $key => $value) {
            if (! in_array($key, self::$allowedSettings)) {
                unset($data[ $key ]);
            }
        }
    }
}
