<?php

/**
 * HappyFenceNG Total Enterprise Security Plugin
 * HAPPYWPRESS is a RazorVue Trademark.
 *
 * @package     HappyFenceNG
 * @developer   Simonluca Scillitani
 * @copyright   2025 RazorVue di Scillitani Simonluca
 * @link        https://razorvue.it
 * @author      RazorVue di Scillitani Simonluca
 * @brand       HappyWPress
 * @uri         https://happywpress.com
 * @set all
 */
if ( !defined( 'ABSPATH' ) ) {
    exit;
}
/**PHPCS -> code compliance: we put useful comments where needed.. */
class HappyFenceNGVRMLOG extends HappyFenceNGAVRoute {
    /**PHPCS -> code compliance: we put useful comments where needed.. */
    public function doRoute() {
        try {
            $nonce_save_settings = wp_create_nonce( 'happyfenceng_MLOG_save_settings' );
            $nonce_paging = wp_create_nonce( 'happyfenceng_MLOG_paging' );
            $burn = HappyFenceNGFailFast::setup();
            $allowed_sev = HappyFenceNGCleaner::checkMe( HappyFenceNGWhoAmI::get_instance()->LOGS->allowed_severity, HappyFenceNGPlaceholder::EMPTY_ARRAY );
            $allowed_fac = HappyFenceNGCleaner::checkMe( HappyFenceNGWhoAmI::get_instance()->LOGS->allowed_facility, HappyFenceNGPlaceholder::EMPTY_ARRAY );
            $severities = array();
            $severities[] = array(
                'name'       => HappyFenceNGPlaceholder::ALL_SEVERITIES,
                'is_checked' => false,
                'is_enabled' => function_exists( 'htes_fs' ) && HappyFenceNGTR::is_in( static::class, array(HappyFenceNGTR::PREAMBLE) ),
            );
            foreach ( HappyFenceNGSyslogSeverity::cases() as $sev ) {
                $is_checked = in_array( $sev, $allowed_sev, true );
                $severities[] = array(
                    'name'       => $sev->name,
                    'is_checked' => $is_checked,
                    'is_enabled' => function_exists( 'htes_fs' ) && HappyFenceNGTR::is_in( static::class, array(HappyFenceNGTR::PREAMBLE) ),
                );
            }
            $facilities = array();
            $facilities[] = array(
                'name'       => HappyFenceNGPlaceholder::ALL_FACILITIES,
                'is_checked' => false,
                'is_enabled' => function_exists( 'htes_fs' ) && HappyFenceNGTR::is_in( static::class, array(HappyFenceNGTR::PREAMBLE) ),
            );
            foreach ( HappyFenceNGSyslogFacility::cases() as $fac ) {
                $is_checked = in_array( $fac, $allowed_fac, true );
                $facilities[] = array(
                    'name'       => $fac->name,
                    'is_checked' => $is_checked,
                    'is_enabled' => function_exists( 'htes_fs' ) && HappyFenceNGTR::is_in( static::class, array(HappyFenceNGTR::BUNKER) ),
                );
            }
            $filters = array();
            $total = 0;
            $total_pages = ceil( $total / 1 );
            $siem_endpoint = 'not_supported';
            $params = array(
                'texts'  => array(
                    'title'                     => esc_html__( 'Log Settings', 'happyfenceng-total-enterprise-security' ),
                    'logtype_title'             => esc_html__( 'Log Type', 'happyfenceng-total-enterprise-security' ),
                    'file_logs'                 => esc_html__( 'File Logs', 'happyfenceng-total-enterprise-security' ),
                    'database_logs'             => esc_html__( 'Database Logs', 'happyfenceng-total-enterprise-security' ),
                    'siem_logs'                 => esc_html__( 'Siem Logs', 'happyfenceng-total-enterprise-security' ),
                    'siem_endpoint'             => esc_html__( 'Syslog endpoint (host:port)', 'happyfenceng-total-enterprise-security' ),
                    'siem_endpoint_description' => esc_html__( 'Enter the destination for syslog data.', 'happyfenceng-total-enterprise-security' ),
                    'severity_requires'         => esc_html__( ' (REQUIRES PRO OR BETTER)', 'happyfenceng-total-enterprise-security' ),
                    'facility_requires'         => esc_html__( ' (REQUIRES ENTERPRISE)', 'happyfenceng-total-enterprise-security' ),
                    'log_severities_title'      => esc_html__( 'Log Severities to record', 'happyfenceng-total-enterprise-security' ),
                    'log_facilities_title'      => esc_html__( 'Log Facilities to record', 'happyfenceng-total-enterprise-security' ),
                    'log_records'               => esc_html__( 'Log Records (Database)', 'happyfenceng-total-enterprise-security' ),
                    'important'                 => esc_html__( 'Important:', 'happyfenceng-total-enterprise-security' ),
                    'disclaimer_main'           => esc_html__( 'HappyFenceNG does not include a "Clear Logs" feature. All log data is considered audit-sensitive and must be managed by authorized personnel.', 'happyfenceng-total-enterprise-security' ),
                    'disclaimer_description'    => esc_html__( 'Log records are stored for auditing and compliance. Export and deletion are intentionally disabled to preserve integrity. For full access, use SIEM or connect via database.', 'happyfenceng-total-enterprise-security' ),
                    'no_records_found'          => esc_html__( 'No results:try with SHOW LOGS button.', 'happyfenceng-total-enterprise-security' ),
                    'timestamp_title'           => esc_html__( 'Timestamp', 'happyfenceng-total-enterprise-security' ),
                    'severity_title'            => esc_html__( 'Severity', 'happyfenceng-total-enterprise-security' ),
                    'facility_title'            => esc_html__( 'Facility', 'happyfenceng-total-enterprise-security' ),
                    'all_severities'            => esc_html__( 'All Severities', 'happyfenceng-total-enterprise-security' ),
                    'all_facilities'            => esc_html__( 'All Facilities', 'happyfenceng-total-enterprise-security' ),
                    'message_title'             => esc_html__( 'Message', 'happyfenceng-total-enterprise-security' ),
                    'filter_title'              => esc_html__( 'Filter', 'happyfenceng-total-enterprise-security' ),
                    'show_logs_title'           => esc_html__( 'Show logs', 'happyfenceng-total-enterprise-security' ),
                    'feature_requires'          => esc_html__( 'SOME FEATURES REQUIRES PREMIUM', 'happyfenceng-total-enterprise-security' ),
                    'save_settings'             => esc_html__( 'Save Settings', 'happyfenceng-total-enterprise-security' ),
                    'settings_saved'            => esc_html__( 'Settings Saved', 'happyfenceng-total-enterprise-security' ),
                    'error_saving_settings'     => esc_html__( 'Error saving settings', 'happyfenceng-total-enterprise-security' ),
                ),
                'links'  => array(
                    'happyfenceng_logo_url' => esc_url( HAPPYFENCENG_PLUGIN_URL . 'assets/images/happyfenceng-small.png' ),
                ),
                'values' => array(
                    'log_type'         => HappyFenceNGCleaner::checkMe( HappyFenceNGWhoAmI::get_instance()->LOGS->log_type, HappyFenceNGWhoAmI::get_instance()->LOGS::_DEF_LOG_TYPE ),
                    'enabled_db'       => function_exists( 'htes_fs' ) && HappyFenceNGTR::is_in( static::class, array(HappyFenceNGTR::AGGREGATE) ),
                    'enabled_siem'     => function_exists( 'htes_fs' ) && HappyFenceNGTR::is_in( static::class, array(HappyFenceNGTR::BUNKER) ),
                    'enabled_severity' => function_exists( 'htes_fs' ) && HappyFenceNGTR::is_in( static::class, array(HappyFenceNGTR::PREAMBLE) ),
                    'enabled_facility' => function_exists( 'htes_fs' ) && HappyFenceNGTR::is_in( static::class, array(HappyFenceNGTR::BUNKER) ),
                    'siem_endpoint'    => $siem_endpoint,
                    'all_severities'   => $severities,
                    'all_facilities'   => $facilities,
                    'checkbox_on'      => HappyFenceNGPlaceholder::CHECKBOX_ON,
                    'checkbox_off'     => HappyFenceNGPlaceholder::CHECKBOX_OFF,
                    'nonce'            => $nonce_save_settings,
                    'noncePaging'      => $nonce_paging,
                    'totalPages'       => $total_pages,
                    'burnNonce'        => $burn,
                    'feature_enabled'  => function_exists( 'htes_fs' ) && HappyFenceNGTR::is_in( static::class, array(HappyFenceNGTR::PREAMBLE) ),
                ),
            );
            ob_start();
            HappyFenceNGVControl::renderView( $params, 'mlog' );
            // phpcs:ignore WordPress.Security.EscapeOutput
            echo ob_get_clean();
        } catch ( Throwable $e ) {
            HappyFenceNGLOGCORE::logcore( 'HappyFenceNGVRMLOG - cannot render UI ' . $e->getMessage(), HappyFenceNGSyslogFacility::UI, HappyFenceNGSyslogSeverity::ERROR );
            ob_end_clean();
            HappyFenceNGCleaner::cleaner_oops();
        }
    }

}
