<?php
/**
 * NotificationsSettings — Notification configuration settings page.
 *
 * Tabs:
 *   General        — master switch, admin-login notification
 *   Email          — from address/name, default recipient
 *   Integrations   — webhook URL/format/secret, Slack incoming webhook
 *   Throttling     — digest mode, cooldown, max-per-hour
 *   Logs           — log enabled, retention period, retry-failed
 *
 * @package UltraguardPro\modules\notifications\pages
 */

namespace UltraguardPro\modules\notifications\pages;

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

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

class NotificationsSettings 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' );

        $email_on   = (bool) $s( 'notifications_email_enabled',   true  );
        $webhook_on = (bool) $s( 'notifications_webhook_enabled', false );
        $slack_on   = (bool) $s( 'notifications_slack_enabled',   false );
        $any_on     = $email_on || $webhook_on || $slack_on;

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

            'header' => [
                'title'    => __( 'Notification Settings', 'ultraguard-security-suite' ),
                'subtitle' => __( 'Configure email, webhook and Slack delivery channels, throttling and log retention.', 'ultraguard-security-suite' ),
                'badge'    => [
                    'text'       => $any_on ? __( 'Channels Active', 'ultraguard-security-suite' ) : __( 'All Channels Off', 'ultraguard-security-suite' ),
                    'badge_type' => $any_on ? 'success' : 'secondary',
                ],
                'actions' => [
                    $this->create_ajax_button( __( 'Send Test Email', 'ultraguard-security-suite' ), 'ultraguard_notif_send_test',
                        [ 'channel' => 'email',   'recipient' => get_option( 'admin_email' ) ], 'secondary', 'dashicons-email-alt'  ),
                    $this->create_ajax_button( __( 'Test Webhook',    'ultraguard-security-suite' ), 'ultraguard_notif_send_test',
                        [ 'channel' => 'webhook', 'recipient' => $s( 'notifications_webhook_url', '' ) ], 'secondary', 'dashicons-admin-links' ),
                    $this->create_ajax_button( __( 'Test Slack',      'ultraguard-security-suite' ), 'ultraguard_notif_send_test',
                        [ 'channel' => 'slack',   'recipient' => $s( 'notifications_slack_webhook', '' ) ], 'secondary', 'dashicons-format-chat' ),
                ],
            ],

            'data' => [ [
                'type' => 'tabs',
                'id'   => 'notifications-settings-tabs',
                'tabs' => [

                    // ── Tab 1: General ───────────────────────────────────
                    [
                        'id'    => 'general',
                        'title' => __( 'General', 'ultraguard-security-suite' ),
                        'icon'  => 'dashicons-bell',
                        'data'  => [ [
                            'type'    => 'grid',
                            'columns' => 12,
                            'gap'     => '20px',
                            'items'   => [
                                [ 'span' => 12, 'component' => [
                                    'type'  => 'card',
                                    'title' => __( 'Global Settings', 'ultraguard-security-suite' ),
                                    'icon'  => 'dashicons-bell',
                                    'data'  => [
                                        [ 'type' => 'toggle', 'name' => 'notifications_enabled',        'label' => __( 'Enable Notifications',  'ultraguard-security-suite' ), 'description' => __( 'Master switch — turn off to silence all notifications.', 'ultraguard-security-suite' ), 'default' => (bool) $s( 'notifications_enabled', true ) ],
                                        [ 'type' => 'toggle', 'name' => 'notifications_notify_on_login', 'label' => __( 'Notify on Admin Login', 'ultraguard-security-suite' ), 'description' => __( 'Send an info-level notification when an admin logs in.',    'ultraguard-security-suite' ), 'default' => (bool) $s( 'notifications_notify_on_login', false ) ],
                                    ],
                                ] ],
                            ],
                        ] ],
                    ],

                    // ── Tab 2: Email ─────────────────────────────────────
                    [
                        'id'    => 'email',
                        'title' => __( 'Email', 'ultraguard-security-suite' ),
                        'icon'  => 'dashicons-email',
                        'data'  => [ [
                            'type'    => 'grid',
                            'columns' => 12,
                            'gap'     => '20px',
                            'items'   => [
                                [ 'span' => 12, 'component' => [
                                    'type'  => 'card',
                                    'title' => __( 'Email Notifications', 'ultraguard-security-suite' ),
                                    'icon'  => 'dashicons-email',
                                    'badge' => [ 'text' => $email_on ? __( 'Enabled', 'ultraguard-security-suite' ) : __( 'Disabled', 'ultraguard-security-suite' ), 'badge_type' => $email_on ? 'success' : 'secondary' ],
                                    'data'  => [
                                        [ 'type' => 'toggle', 'name' => 'notifications_email_enabled', 'label' => __( 'Enable Email Alerts',       'ultraguard-security-suite' ), 'default' => $email_on ],
                                        [ 'type' => 'text',   'name' => 'notifications_from_email',    'label' => __( 'From Email Address',         'ultraguard-security-suite' ), 'placeholder' => 'no-reply@' . preg_replace( '/^www\./i', '', (string) wp_parse_url( home_url(), PHP_URL_HOST ) ), 'description' => __( 'Notification emails should use a same-domain sender address.', 'ultraguard-security-suite' ), 'default' => $s( 'notifications_from_email'  ) ],
                                        [ 'type' => 'text',   'name' => 'notifications_from_name',     'label' => __( 'From Display Name',          'ultraguard-security-suite' ), 'placeholder' => get_bloginfo( 'name' ), 'description' => __( 'Display name shown in email clients for notification emails.', 'ultraguard-security-suite' ), 'default' => $s( 'notifications_from_name'   ) ],
                                        [ 'type' => 'text',   'name' => 'notifications_admin_email',   'label' => __( 'Default Alert Recipient',    'ultraguard-security-suite' ), 'placeholder' => get_option( 'admin_email' ),              'description' => __( 'Default recipient when a rule has no specific recipient.', 'ultraguard-security-suite' ), 'default' => $s( 'notifications_admin_email' ) ],
                                    ],
                                ] ],
                            ],
                        ] ],
                    ],

                    // ── Tab 3: Integrations ──────────────────────────────
                    [
                        'id'    => 'integrations',
                        'title' => __( 'Integrations', 'ultraguard-security-suite' ),
                        'icon'  => 'dashicons-admin-links',
                        'data'  => [ [
                            'type'    => 'grid',
                            'columns' => 12,
                            'gap'     => '20px',
                            'items'   => [
                                [ 'span' => 12, 'component' => [
                                    'type'  => 'card',
                                    'title' => __( 'Webhook & Slack', 'ultraguard-security-suite' ),
                                    'icon'  => 'dashicons-admin-links',
                                    'data'  => [
                                        [ 'type' => 'toggle', 'name' => 'notifications_webhook_enabled', 'label' => __( 'Enable Webhook Delivery',     'ultraguard-security-suite' ), 'default' => $webhook_on ],
                                        [ 'type' => 'text',   'name' => 'notifications_webhook_url',     'label' => __( 'Default Webhook URL',         'ultraguard-security-suite' ), 'placeholder' => 'https://your-endpoint.com/hook', 'default' => $s( 'notifications_webhook_url' ) ],
                                        [
                                            'type'    => 'select',
                                            'name'    => 'notifications_webhook_format',
                                            'label'   => __( 'Payload Format', 'ultraguard-security-suite' ),
                                            'options' => [ 'json' => 'JSON', 'slack' => 'Slack (Block Kit)', 'form' => 'Form Encoded' ],
                                            'default' => $s( 'notifications_webhook_format', 'json' ),
                                        ],
                                        [ 'type' => 'text',   'name' => 'notifications_webhook_secret',  'label' => __( 'Webhook Signing Secret',      'ultraguard-security-suite' ), 'placeholder' => __( 'Optional HMAC-SHA256 key', 'ultraguard-security-suite' ), 'description' => __( 'Adds X-UltraGuard-Signature header to all webhook POSTs.', 'ultraguard-security-suite' ), 'default' => $s( 'notifications_webhook_secret' ) ],
                                        [ 'type' => 'toggle', 'name' => 'notifications_slack_enabled',   'label' => __( 'Enable Slack Integration',    'ultraguard-security-suite' ), 'default' => $slack_on ],
                                        [ 'type' => 'text',   'name' => 'notifications_slack_webhook',   'label' => __( 'Slack Incoming Webhook URL',  'ultraguard-security-suite' ), 'placeholder' => 'https://hooks.slack.com/services/…', 'default' => $s( 'notifications_slack_webhook' ) ],
                                    ],
                                ] ],
                            ],
                        ] ],
                    ],

                    // ── Tab 4: Throttling ────────────────────────────────
                    [
                        'id'    => 'throttling',
                        'title' => __( 'Throttling', 'ultraguard-security-suite' ),
                        'icon'  => 'dashicons-clock',
                        'data'  => [ [
                            'type'    => 'grid',
                            'columns' => 12,
                            'gap'     => '20px',
                            'items'   => [
                                [ 'span' => 12, 'component' => [
                                    'type'  => 'card',
                                    'title' => __( 'Throttling & Digest', 'ultraguard-security-suite' ),
                                    'icon'  => 'dashicons-clock',
                                    'data'  => [
                                        [ 'type' => 'toggle', 'name' => 'notifications_digest_mode',      'label' => __( 'Digest Mode',              'ultraguard-security-suite' ), 'description' => __( 'Bundle non-critical notifications into periodic summaries.', 'ultraguard-security-suite' ), 'default' => (bool) $s( 'notifications_digest_mode', false ) ],
                                        [
                                            'type'    => 'select',
                                            'name'    => 'notifications_digest_frequency',
                                            'label'   => __( 'Digest 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( 'notifications_digest_frequency', 'daily' ),
                                        ],
                                        [ 'type' => 'number', 'name' => 'notifications_global_cooldown',  'label' => __( 'Global Cooldown (minutes)', 'ultraguard-security-suite' ), 'min' => 0, 'max' => 1440, 'description' => __( 'Minimum gap between same-type alerts (0 = off).', 'ultraguard-security-suite' ), 'default' => (int) $s( 'notifications_global_cooldown', 60 ) ],
                                        [ 'type' => 'number', 'name' => 'notifications_max_per_hour',     'label' => __( 'Max Alerts / Hour',         'ultraguard-security-suite' ), 'min' => 1, 'max' => 1000, 'description' => __( 'Rate limit — excess alerts are silently dropped.', 'ultraguard-security-suite' ), 'default' => (int) $s( 'notifications_max_per_hour', 50 ) ],
                                    ],
                                ] ],
                            ],
                        ] ],
                    ],

                    // ── Tab 5: Logs ──────────────────────────────────────
                    [
                        'id'    => 'logs',
                        'title' => __( 'Logs', 'ultraguard-security-suite' ),
                        'icon'  => 'dashicons-list-view',
                        'data'  => [ [
                            'type'    => 'grid',
                            'columns' => 12,
                            'gap'     => '20px',
                            'items'   => [
                                [ 'span' => 12, 'component' => [
                                    'type'  => 'card',
                                    'title' => __( 'Log & Retention', 'ultraguard-security-suite' ),
                                    'icon'  => 'dashicons-list-view',
                                    'data'  => [
                                        [ 'type' => 'toggle', 'name' => 'notifications_log_enabled',   'label' => __( 'Enable Notification Log',   'ultraguard-security-suite' ), 'description' => __( 'Store a record of every notification sent or failed.', 'ultraguard-security-suite' ), 'default' => (bool) $s( 'notifications_log_enabled',  true  ) ],
                                        [ 'type' => 'number', 'name' => 'notifications_log_retention', 'label' => __( 'Log Retention (days)',       'ultraguard-security-suite' ), 'min' => 1, 'max' => 365, 'description' => __( 'Automatically remove entries older than N days.', 'ultraguard-security-suite' ), 'default' => (int) $s( 'notifications_log_retention', 90   ) ],
                                        [ 'type' => 'toggle', 'name' => 'notifications_retry_failed',  'label' => __( 'Auto-Retry Failed (10 min)', 'ultraguard-security-suite' ), 'description' => __( 'Automatically retry failed notifications once after 10 minutes.', 'ultraguard-security-suite' ), 'default' => (bool) $s( 'notifications_retry_failed', true  ) ],
                                    ],
                                ] ],
                            ],
                        ] ],
                    ],

                ],
            ] ],

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