<?php

namespace UltraguardPro\modules\reputation_monitor\pages;

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

use UltraguardPro\Plugin;
use UltraguardPro\modules\core\BasePage;

class ReputationMonitorSettings extends BasePage {

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

    public function get_scheme( $context ): array {
        $settings = $this->settings->get_module_settings( 'reputation_monitor' );
        $nonce    = $this->create_nonce( 'ultraguard_admin_nonce' );
        $users    = get_users( [ 'role__in' => [ 'administrator' ], 'fields' => [ 'ID', 'display_name', 'user_email' ] ] );
        $options  = [];

        foreach ( $users as $user ) {
            $options[] = [
                'value'    => (string) $user->user_email,
                'label'    => $user->display_name . ' (' . $user->user_email . ')',
                'selected' => in_array( (string) $user->user_email, (array) ( $settings['reputation_monitor_alert_recipients'] ?? [] ), true ),
            ];
        }

        return [
            'type'      => 'page',
            'id'        => 'reputation-monitor-settings',
            'page_slug' => 'reputation-monitor-settings',
            'header'    => [
                'title'    => __( 'Reputation Monitor Settings', 'ultraguard-security-suite' ),
                'subtitle' => __( 'Configure blacklist sources, check frequency, and alert routing for reputation issues.', 'ultraguard-security-suite' ),
                'actions'  => [
                    $this->create_url_button( __( 'Back to Dashboard', 'ultraguard-security-suite' ), $this->get_page_url( 'reputation-monitor' ), 'secondary', 'dashicons-arrow-left-alt' ),
                ],
            ],
            'data'      => [
                [
                    'type'  => 'card',
                    'title' => __( 'General', 'ultraguard-security-suite' ),
                    'data'  => [
                        [ 'type' => 'toggle', 'name' => 'reputation_monitor_enabled', 'label' => __( 'Enable Reputation Monitor module', 'ultraguard-security-suite' ), 'value' => ! empty( $settings['reputation_monitor_enabled'] ) ],
                        [ 'type' => 'select', 'name' => 'reputation_monitor_frequency', 'label' => __( 'Check frequency', 'ultraguard-security-suite' ), 'value' => (string) ( $settings['reputation_monitor_frequency'] ?? 'hourly' ), 'options' => [
                            [ 'value' => 'every_30_minutes', 'label' => __( 'Every 30 minutes', 'ultraguard-security-suite' ) ],
                            [ 'value' => 'hourly', 'label' => __( 'Every 1 hour', 'ultraguard-security-suite' ) ],
                            [ 'value' => 'every_3_hours', 'label' => __( 'Every 3 hours', 'ultraguard-security-suite' ) ],
                            [ 'value' => 'every_6_hours', 'label' => __( 'Every 6 hours', 'ultraguard-security-suite' ) ],
                            [ 'value' => 'every_12_hours', 'label' => __( 'Every 12 hours', 'ultraguard-security-suite' ) ],
                            [ 'value' => 'daily', 'label' => __( 'Every 24 hours', 'ultraguard-security-suite' ) ],
                        ] ],
                    ],
                ],
                [
                    'type'  => 'card',
                    'title' => __( 'Source Configuration', 'ultraguard-security-suite' ),
                    'data'  => [
                        [ 'type' => 'text', 'name' => 'reputation_monitor_google_api_key', 'label' => __( 'Google Safe Browsing API Key', 'ultraguard-security-suite' ), 'value' => (string) ( $settings['reputation_monitor_google_api_key'] ?? '' ), 'description' => __( 'Get a key from Google Cloud Console.', 'ultraguard-security-suite' ) ],
                        [ 'type' => 'toggle', 'name' => 'reputation_monitor_enable_spamhaus', 'label' => __( 'Enable Spamhaus check', 'ultraguard-security-suite' ), 'value' => ! empty( $settings['reputation_monitor_enable_spamhaus'] ) ],
                        [ 'type' => 'toggle', 'name' => 'reputation_monitor_enable_mxtoolbox', 'label' => __( 'Enable MXToolBox check', 'ultraguard-security-suite' ), 'value' => ! empty( $settings['reputation_monitor_enable_mxtoolbox'] ) ],
                        [ 'type' => 'text', 'name' => 'reputation_monitor_mxtoolbox_api_key', 'label' => __( 'MXToolBox API Key', 'ultraguard-security-suite' ), 'value' => (string) ( $settings['reputation_monitor_mxtoolbox_api_key'] ?? '' ) ],
                        [ 'type' => 'toggle', 'name' => 'reputation_monitor_enable_mcafee', 'label' => __( 'Enable McAfee WebAdvisor check', 'ultraguard-security-suite' ), 'value' => ! empty( $settings['reputation_monitor_enable_mcafee'] ) ],
                        [ 'type' => 'toggle', 'name' => 'reputation_monitor_enable_norton', 'label' => __( 'Enable Norton Safe Web check', 'ultraguard-security-suite' ), 'value' => ! empty( $settings['reputation_monitor_enable_norton'] ) ],
                        [ 'type' => 'toggle', 'name' => 'reputation_monitor_enable_bing', 'label' => __( 'Enable Bing safety check', 'ultraguard-security-suite' ), 'value' => ! empty( $settings['reputation_monitor_enable_bing'] ) ],
                    ],
                ],
                [
                    'type'  => 'card',
                    'title' => __( 'Alerting', 'ultraguard-security-suite' ),
                    'data'  => [
                        [ 'type' => 'toggle', 'name' => 'reputation_monitor_email_alert', 'label' => __( 'Send email alert immediately when blacklisted', 'ultraguard-security-suite' ), 'value' => ! empty( $settings['reputation_monitor_email_alert'] ) ],
                        [ 'type' => 'toggle', 'name' => 'reputation_monitor_weekly_report', 'label' => __( 'Send weekly reputation report', 'ultraguard-security-suite' ), 'value' => ! empty( $settings['reputation_monitor_weekly_report'] ) ],
                        [ 'type' => 'select', 'multiple' => true, 'size' => 4, 'name' => 'reputation_monitor_alert_recipients', 'label' => __( 'Alert recipients', 'ultraguard-security-suite' ), 'value' => (array) ( $settings['reputation_monitor_alert_recipients'] ?? [] ), 'options' => $options ],
                        [ 'type' => 'toggle', 'name' => 'reputation_monitor_webhook_alert', 'label' => __( 'Auto-notify via webhook on blacklisting event', 'ultraguard-security-suite' ), 'value' => ! empty( $settings['reputation_monitor_webhook_alert'] ) ],
                        [ 'type' => 'text', 'name' => 'reputation_monitor_webhook_url', 'label' => __( 'Webhook URL', 'ultraguard-security-suite' ), 'value' => (string) ( $settings['reputation_monitor_webhook_url'] ?? '' ) ],
                    ],
                ],
            ],
            'form'      => [
                'enable' => true,
                'id'     => 'reputation-monitor-settings-form',
                'ajax'   => true,
                'action' => 'ultraguard_save_reputation_monitor_settings',
                'nonce'  => $nonce,
                'submit' => [
                    'text'         => __( 'Save Settings', 'ultraguard-security-suite' ),
                    'button_style' => 'primary',
                    'icon'         => 'dashicons-saved',
                ],
            ],
        ];
    }
}
