<?php

namespace Itgalaxy\Elementor\Form\Infusionsoft\Integration\Admin\AjaxActions;

use Itgalaxy\Elementor\Form\Infusionsoft\Integration\Includes\Bootstrap;

class LogAjaxAction
{
    /**
     * @var string
     */
    public static $enableLogActionName = 'itglx/elpro/keap/enable-log';

    /**
     * @var string
     */
    public static $clearLogActionName = 'itglx/elpro/keap/clear-log';

    public function __construct()
    {
        add_action('wp_ajax_' . self::$clearLogActionName, [$this, 'clear']);
        add_action('wp_ajax_' . self::$enableLogActionName, [$this, 'enable']);
    }

    /**
     * @return void
     */
    public function clear()
    {
        if (!current_user_can('manage_options')) {
            exit;
        }

        Bootstrap::$common->logger->ajaxLogsClear();
    }

    /**
     * @return void
     */
    public function enable()
    {
        if (!current_user_can('manage_options')) {
            exit;
        }

        $settings = get_option(Bootstrap::OPTIONS_KEY, []);

        if (!is_array($settings)) {
            $settings = [];
        }

        $settings['enabled_logging'] = isset($_POST['value']) ? (int) $_POST['value'] : '';

        update_option(Bootstrap::OPTIONS_KEY, $settings);

        \wp_send_json_success(
            [
                'message' => $settings['enabled_logging']
                    ? \esc_html__('Logging enabled successfully', 'el-pro-form-infusionsoft-integration')
                    : \esc_html__('Logging disabled successfully', 'el-pro-form-infusionsoft-integration'),
            ]
        );
    }
}
