<?php

namespace GriffinForms\Admin\Secure;

use GriffinForms\Admin\Secure\Format;

class General extends \GriffinForms\Admin\Secure\Settings
{
    /**
     * Secure the required_indicator_mode setting (radio).
     *
     * @param mixed $value Incoming value.
     * @return string 'asterisk' or 'custom_text'; defaults to 'asterisk'.
     */
    protected function securerequiredindicatormode($value): string
    {
        $val     = is_string($value) ? sanitize_text_field($value) : '';
        $allowed = ['asterisk', 'custom_text'];
        return in_array($val, $allowed, true) ? $val : 'asterisk';
    }

    /**
     * Secure the required_indicator_custom_text setting.
     * Stores 'none' sentinel when user saves blank to avoid deleteOption().
     *
     * @param mixed $value Incoming value.
     * @return string Sanitized text or 'none' sentinel.
     */
    protected function securerequiredindicatorcustomtext($value): string
    {
        $val = is_string($value) ? sanitize_text_field($value) : '';
        return $val === '' ? 'none' : $val;
    }

    /**
     * Secure the delete_data_on_uninstall setting.
     *
     * @param mixed $value The submitted value from settings.
     * @return int Sanitized 1 or 0 depending on checkbox status.
     */
    protected function secureDeleteDataOnUninstall($value): int
    {
        return !empty($value) ? 1 : 0;
    }

    /**
     * Secure the enable_native_logging setting (checkbox).
     *
     * @param mixed $value Incoming value from JS.
     * @return int 1 if enabled, 0 otherwise.
     */
    protected function secureEnableNativeLogging($value): int
    {
        return !empty($value) ? 1 : 0;
    }

    /**
     * Secure the exclude_admin_submissions_from_lifetime_count setting (checkbox).
     *
     * @param mixed $value Incoming value from JS.
     * @return int 1 if enabled, 0 otherwise.
     */
    protected function secureExcludeAdminSubmissionsFromLifetimeCount($value): int
    {
        return !empty($value) ? 1 : 0;
    }

    /**
     * Secure the log_message_types setting (multi-select checkboxes).
     *
     * @param mixed $value Incoming value from JS
     */
    protected function secureLogMessageTypes($value): array
    {
        // Normalize to array
        $incoming = is_array($value) ? $value : (array) $value;

        // Allowed values: value=>label keys from Choices
        $allowed = $this->choices->getChoices('log_message_types', false);

        // Sanitize, filter to allowed, ensure uniqueness and reindex
        $sanitized = array_values(array_unique(array_intersect(array_map('sanitize_text_field', $incoming), $allowed)));
        return $sanitized;
    }

    /**
     * Secure the log_retention setting (select dropdown).
     *
     * @param mixed $value Incoming value from JS.
     * @return string One of the allowed retention keys; defaults to 'latest_10000'.
     */
    protected function secureLogRetention($value): string
    {
        $val = is_string($value) ? sanitize_text_field($value) : '';
        // Choices returns an indexed array of allowed keys for retention
        $allowed = $this->choices->getChoices('log_retention', false);
        if (!in_array($val, $allowed, true)) {
            $val = 'latest_10000';
        }
        return $val;
    }
}
