<?php
/**
 * FilecheckerSettings — File Integrity Checker settings page.
 *
 * Tabs:
 *   Scope       — what to monitor (core, plugins, themes, uploads)
 *   Schedule    — check frequency and alert triggers
 *   Detection   — malware patterns, WordPress.org checksum comparison
 *   Exclusions  — paths and extensions to skip
 *   Advanced    — hash algorithm and max file size
 *
 * @package UltraguardPro\modules\filechecker\pages
 */

namespace UltraguardPro\modules\filechecker\pages;

use UltraguardPro\Plugin;
use UltraguardPro\modules\core\BasePage;
use UltraguardPro\support\UpgradeManager;
use UltraguardPro\support\LicenseInfo;

if ( ! defined( 'ABSPATH' ) ) { exit; }

class FilecheckerSettings extends BasePage {

    public function __construct( Plugin $plugin ) {
        parent::__construct( $plugin );
        $this->ajax_nonce_action = 'ultraguard_admin_nonce';
    }

    public function get_scheme( $context ): array {
        $s     = fn( string $k, $d = '' ) => $this->get_setting( $k, $d );
        $nonce = $this->create_nonce( 'ultraguard_admin_nonce' );

        $is_enabled = (bool) $s( 'filechecker_enabled', true );

        // Contextual Pro nudge (free users only) — placed before tabs
        $nudge = UpgradeManager::should_show_nudge()
            ? [ UpgradeManager::inline_hint( 'file_integrity_scan', __( 'Configure the settings below and upgrade to run your first scan.', 'ultraguard-security-suite' ) ) ]
            : [];

        return apply_filters( 'ultraguard_filechecker_settings_scheme', [
            'type'      => 'page',
            'id'        => 'ultraguard-filechecker-settings',
            'page_slug' => 'filechecker-settings',
            'class'     => 'ultraguard-filechecker-settings-page',

            'header' => [
                'title'    => __( 'File Integrity Checker Settings', 'ultraguard-security-suite' ),
                'subtitle' => __( 'Configure scan scope, scheduling, malware detection, and alert preferences.', 'ultraguard-security-suite' ),
                'badge'    => [
                    'text'       => $is_enabled ? __( 'Active', 'ultraguard-security-suite' ) : __( 'Inactive', 'ultraguard-security-suite' ),
                    'badge_type' => $is_enabled ? 'success' : 'secondary',
                ],
                'actions' => [
                    $this->create_ajax_button(
                        __( 'Run Integrity Check Now', 'ultraguard-security-suite' ),
                        'ultraguard_fc_run_integrity_check', [], 'secondary', 'dashicons-search'
                    ),
                    $this->create_ajax_button(
                        __( 'Reset Baseline', 'ultraguard-security-suite' ),
                        'ultraguard_fc_reset_baseline', [], 'warning', 'dashicons-image-rotate',
                        __( 'Mark all current file hashes as the new trusted baseline?', 'ultraguard-security-suite' )
                    ),
                ],
            ],

            'data' => array_merge( $nudge, [ [
                'type' => 'tabs',
                'id'   => 'filechecker-settings-tabs',
                'tabs' => [

                    // ── Tab 1: Scope ─────────────────────────────────────
                    [
                        'id'    => 'scope',
                        'title' => __( 'Scope', 'ultraguard-security-suite' ),
                        'icon'  => 'dashicons-media-text',
                        'data'  => [ [
                            'type'    => 'grid',
                            'columns' => 12,
                            'gap'     => '20px',
                            'items'   => [
                                [ 'span' => 12, 'component' => [
                                    'type'  => 'card',
                                    'title' => __( 'Monitoring Scope', 'ultraguard-security-suite' ),
                                    'icon'  => 'dashicons-media-text',
                                    'data'  => [
                                        [ 'type' => 'toggle', 'name' => 'filechecker_enabled',      'label' => __( 'Enable File Checker',           'ultraguard-security-suite' ), 'description' => __( 'Activate automated file integrity monitoring.', 'ultraguard-security-suite' ), 'default' => $s( 'filechecker_enabled', true ) ],
                                        [ 'type' => 'toggle', 'name' => 'filechecker_scan_core',    'label' => __( 'Monitor WP Core Files',         'ultraguard-security-suite' ), 'description' => __( 'wp-includes and wp-admin directories.',         'ultraguard-security-suite' ), 'default' => $s( 'filechecker_scan_core',    true  ) ],
                                        [ 'type' => 'toggle', 'name' => 'filechecker_scan_plugins', 'label' => __( 'Monitor Plugin Files',          'ultraguard-security-suite' ), 'description' => __( 'All installed plugin directories.',               'ultraguard-security-suite' ), 'default' => $s( 'filechecker_scan_plugins', true  ) ],
                                        [ 'type' => 'toggle', 'name' => 'filechecker_scan_themes',  'label' => __( 'Monitor Theme Files',           'ultraguard-security-suite' ), 'description' => __( 'All installed theme directories.',                'ultraguard-security-suite' ), 'default' => $s( 'filechecker_scan_themes',  true  ) ],
                                        [ 'type' => 'toggle', 'name' => 'filechecker_scan_uploads', 'label' => __( 'Monitor Uploads Directory',     'ultraguard-security-suite' ), 'description' => __( 'Check uploads for unexpected PHP executables.',   'ultraguard-security-suite' ), 'default' => $s( 'filechecker_scan_uploads', false ) ],
                                        [ 'type' => 'toggle', 'name' => 'filechecker_scan_on_update','label'=> __( 'Re-scan After Core/Plugin Update','ultraguard-security-suite'),'description' => __( 'Automatically trigger a scan after upgrades.',     'ultraguard-security-suite' ), 'default' => $s( 'filechecker_scan_on_update', true ) ],
                                    ],
                                ] ],
                            ],
                        ] ],
                    ],

                    // ── Tab 2: Schedule ──────────────────────────────────
                    [
                        'id'    => 'schedule',
                        'title' => __( 'Schedule', 'ultraguard-security-suite' ),
                        'icon'  => 'dashicons-clock',
                        'data'  => [ [
                            'type'    => 'grid',
                            'columns' => 12,
                            'gap'     => '20px',
                            'items'   => [
                                [ 'span' => 12, 'component' => [
                                    'type'  => 'card',
                                    'title' => __( 'Schedule & Alerts', 'ultraguard-security-suite' ),
                                    'icon'  => 'dashicons-clock',
                                    'data'  => [
                                        [ 'type' => 'toggle', 'name' => 'filechecker_scheduled',          'label' => __( 'Enable Scheduled Checks',    'ultraguard-security-suite' ), 'description' => __( 'Automatically run checks on the schedule below.', 'ultraguard-security-suite' ), 'default' => $s( 'filechecker_scheduled', true ) ],
                                        [
                                            'type'    => 'select',
                                            'name'    => 'filechecker_schedule',
                                            'label'   => __( 'Check Frequency', 'ultraguard-security-suite' ),
                                            'options' => [
                                                'hourly'     => __( 'Hourly',      'ultraguard-security-suite' ),
                                                'twicedaily' => __( 'Twice Daily', 'ultraguard-security-suite' ),
                                                'daily'      => __( 'Daily',       'ultraguard-security-suite' ),
                                                'weekly'     => __( 'Weekly',      'ultraguard-security-suite' ),
                                            ],
                                            'default' => $s( 'filechecker_schedule', 'daily' ),
                                        ],
                                        [ 'type' => 'toggle', 'name' => 'filechecker_notify_modified',   'label' => __( 'Alert on Modified Files',  'ultraguard-security-suite' ), 'description' => __( 'Email admin when tracked files are changed.',          'ultraguard-security-suite' ), 'default' => $s( 'filechecker_notify_modified',   true ) ],
                                        [ 'type' => 'toggle', 'name' => 'filechecker_notify_added',      'label' => __( 'Alert on New Files',       'ultraguard-security-suite' ), 'description' => __( 'Email admin when new PHP files are detected.',           'ultraguard-security-suite' ), 'default' => $s( 'filechecker_notify_added',      true ) ],
                                        [ 'type' => 'toggle', 'name' => 'filechecker_notify_suspicious', 'label' => __( 'Alert on Suspicious Files','ultraguard-security-suite' ), 'description' => __( 'Email admin on malware pattern detection.',               'ultraguard-security-suite' ), 'default' => $s( 'filechecker_notify_suspicious', true ) ],
                                    ],
                                ] ],
                            ],
                        ] ],
                    ],

                    // ── Tab 3: Detection ─────────────────────────────────
                    [
                        'id'    => 'detection',
                        'title' => __( 'Detection', 'ultraguard-security-suite' ),
                        'icon'  => 'dashicons-shield-alt',
                        'data'  => [ [
                            'type'    => 'grid',
                            'columns' => 12,
                            'gap'     => '20px',
                            'items'   => [
                                [ 'span' => 12, 'component' => [
                                    'type'  => 'card',
                                    'title' => __( 'Malware Detection', 'ultraguard-security-suite' ),
                                    'icon'  => 'dashicons-shield-alt',
                                    'data'  => [
                                        [ 'type' => 'toggle', 'name' => 'filechecker_malware_scan',    'label' => __( 'Enable Malware Pattern Scan',        'ultraguard-security-suite' ), 'description' => __( 'Scan PHP files for known backdoor and webshell patterns.', 'ultraguard-security-suite' ), 'default' => $s( 'filechecker_malware_scan', true ) ],
                                        [ 'type' => 'toggle', 'name' => 'filechecker_compare_remote',  'label' => __( 'Compare with WordPress.org Checksums','ultraguard-security-suite' ), 'description' => __( 'Verify core files against official WordPress.org hashes.',    'ultraguard-security-suite' ), 'default' => $s( 'filechecker_compare_remote', true ) ],
                                    ],
                                ] ],
                            ],
                        ] ],
                    ],

                    // ── Tab 4: Exclusions ────────────────────────────────
                    [
                        'id'    => 'exclusions',
                        'title' => __( 'Exclusions', 'ultraguard-security-suite' ),
                        'icon'  => 'dashicons-minus',
                        'data'  => [ [
                            'type'    => 'grid',
                            'columns' => 12,
                            'gap'     => '20px',
                            'items'   => [
                                [ 'span' => 12, 'component' => [
                                    'type'  => 'card',
                                    'title' => __( 'Exclusions', 'ultraguard-security-suite' ),
                                    'icon'  => 'dashicons-minus',
                                    'data'  => [
                                        [
                                            'type'        => 'textarea',
                                            'name'        => 'filechecker_exclude_paths',
                                            'label'       => __( 'Excluded Paths', 'ultraguard-security-suite' ),
                                            'description' => __( 'One path per line. Relative to ABSPATH or absolute.', 'ultraguard-security-suite' ),
                                            'rows'        => 5,
                                            'placeholder' => "wp-content/uploads\nwp-content/cache\nwp-content/backup",
                                            'default'     => $s( 'filechecker_exclude_paths', '' ),
                                        ],
                                        [
                                            'type'        => 'textarea',
                                            'name'        => 'filechecker_exclude_extensions',
                                            'label'       => __( 'Excluded Extensions', 'ultraguard-security-suite' ),
                                            'description' => __( 'Comma-separated list of file extensions to skip.', 'ultraguard-security-suite' ),
                                            'rows'        => 2,
                                            'placeholder' => 'jpg, png, gif, svg, log, tmp, css, js',
                                            'default'     => $s( 'filechecker_exclude_extensions', '' ),
                                        ],
                                    ],
                                ] ],
                            ],
                        ] ],
                    ],

                    // ── Tab 5: Advanced ──────────────────────────────────
                    [
                        'id'    => 'advanced',
                        'title' => __( 'Advanced', 'ultraguard-security-suite' ),
                        'icon'  => 'dashicons-admin-settings',
                        'data'  => [ [
                            'type'    => 'grid',
                            'columns' => 12,
                            'gap'     => '20px',
                            'items'   => [
                                [ 'span' => 12, 'component' => [
                                    'type'  => 'card',
                                    'title' => __( 'Advanced', 'ultraguard-security-suite' ),
                                    'icon'  => 'dashicons-admin-settings',
                                    'data'  => [
                                        [
                                            'type'    => 'select',
                                            'name'    => 'filechecker_hash_algorithm',
                                            'label'   => __( 'Hash Algorithm', 'ultraguard-security-suite' ),
                                            'description' => __( 'Hashing algorithm used to fingerprint files. SHA-256 provides the best security.', 'ultraguard-security-suite' ),
                                            'options' => [
                                                'md5'    => __( 'MD5 (fastest)',          'ultraguard-security-suite' ),
                                                'sha1'   => __( 'SHA-1 (fast)',           'ultraguard-security-suite' ),
                                                'sha256' => __( 'SHA-256 (recommended)', 'ultraguard-security-suite' ),
                                            ],
                                            'default' => $s( 'filechecker_hash_algorithm', 'sha256' ),
                                        ],
                                        [
                                            'type'        => 'number',
                                            'name'        => 'filechecker_max_file_size',
                                            'label'       => __( 'Max File Size to Hash (MB)', 'ultraguard-security-suite' ),
                                            'description' => __( 'Skip hashing files larger than this limit to reduce memory usage.', 'ultraguard-security-suite' ),
                                            'min'         => 1,
                                            'max'         => 500,
                                            'default'     => (int) $s( 'filechecker_max_file_size', 50 ),
                                        ],
                                    ],
                                ] ],
                            ],
                        ] ],
                    ],

                ],
            ] ] ),

            'form' => [
                'enable'     => true,
                'id'         => 'filechecker-settings-form',
                'ajax'       => true,
                'action'     => 'ultraguard_fc_save_settings',
                'nonce'      => $nonce,
                'submit'     => [
                    'text'         => __( 'Save File Checker Settings', 'ultraguard-security-suite' ),
                    'button_style' => 'primary',
                ],
            ],
        ], $context );
    }
}
