<?php

namespace UltraguardPro\modules\uptime_monitor\pages;

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

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

class UptimeMonitorSettings 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( 'uptime_monitor' );
        $nonce    = $this->create_nonce( 'ultraguard_admin_nonce' );

        return [
            'type'      => 'page',
            'id'        => 'uptime-monitor-settings',
            'page_slug' => 'uptime-monitor-settings',
            'header'    => [
                'title'    => __( 'Uptime Monitor Settings', 'ultraguard-security-suite' ),
                'subtitle' => __( 'Configure uptime intervals, monitored URLs, SSL monitoring, and alert behavior.', 'ultraguard-security-suite' ),
                'actions'  => [
                    $this->create_url_button( __( 'Back to Dashboard', 'ultraguard-security-suite' ), $this->get_page_url( 'uptime-monitor' ), 'secondary', 'dashicons-arrow-left-alt' ),
                ],
            ],
            'data'      => [
                [
                    'type'  => 'card',
                    'title' => __( 'Monitoring', 'ultraguard-security-suite' ),
                    'data'  => [
                        [ 'type' => 'toggle', 'name' => 'uptime_monitor_enabled', 'label' => __( 'Enable uptime monitoring', 'ultraguard-security-suite' ), 'value' => ! empty( $settings['uptime_monitor_enabled'] ) ],
                        [ 'type' => 'text', 'name' => 'uptime_monitor_check_url', 'label' => __( 'URL to monitor', 'ultraguard-security-suite' ), 'value' => (string) ( $settings['uptime_monitor_check_url'] ?? home_url() ) ],
                        [ 'type' => 'textarea', 'name' => 'uptime_monitor_additional_urls', 'label' => __( 'Additional URLs to monitor', 'ultraguard-security-suite' ), 'value' => (string) ( $settings['uptime_monitor_additional_urls'] ?? '' ), 'description' => __( 'One URL per line.', 'ultraguard-security-suite' ) ],
                        [ 'type' => 'select', 'name' => 'uptime_monitor_interval', 'label' => __( 'Check interval', 'ultraguard-security-suite' ), 'value' => (string) ( $settings['uptime_monitor_interval'] ?? '5_minutes' ), 'options' => [
                            [ 'value' => '1_minute', 'label' => __( '1 minute', 'ultraguard-security-suite' ) ],
                            [ 'value' => '5_minutes', 'label' => __( '5 minutes', 'ultraguard-security-suite' ) ],
                            [ 'value' => '15_minutes', 'label' => __( '15 minutes', 'ultraguard-security-suite' ) ],
                            [ 'value' => '30_minutes', 'label' => __( '30 minutes', 'ultraguard-security-suite' ) ],
                        ] ],
                    ],
                ],
                [
                    'type'  => 'card',
                    'title' => __( 'SSL Monitoring', 'ultraguard-security-suite' ),
                    'data'  => [
                        [ 'type' => 'toggle', 'name' => 'uptime_monitor_enable_ssl', 'label' => __( 'Enable SSL monitoring', 'ultraguard-security-suite' ), 'value' => ! empty( $settings['uptime_monitor_enable_ssl'] ) ],
                        [ 'type' => 'toggle', 'name' => 'uptime_monitor_ssl_alert_30', 'label' => __( 'Alert when SSL expires in < 30 days', 'ultraguard-security-suite' ), 'value' => ! empty( $settings['uptime_monitor_ssl_alert_30'] ) ],
                        [ 'type' => 'toggle', 'name' => 'uptime_monitor_ssl_alert_7', 'label' => __( 'Alert when SSL expires in < 7 days', 'ultraguard-security-suite' ), 'value' => ! empty( $settings['uptime_monitor_ssl_alert_7'] ) ],
                    ],
                ],
                [
                    'type'  => 'card',
                    'title' => __( 'Alerts', 'ultraguard-security-suite' ),
                    'data'  => [
                        [ 'type' => 'toggle', 'name' => 'uptime_monitor_downtime_email', 'label' => __( 'Send downtime email alert', 'ultraguard-security-suite' ), 'value' => ! empty( $settings['uptime_monitor_downtime_email'] ) ],
                        [ 'type' => 'number', 'name' => 'uptime_monitor_failure_threshold', 'label' => __( 'Alert after X consecutive failures', 'ultraguard-security-suite' ), 'value' => (int) ( $settings['uptime_monitor_failure_threshold'] ?? 2 ) ],
                        [ 'type' => 'toggle', 'name' => 'uptime_monitor_recovery_email', 'label' => __( 'Send recovery email when site comes back up', 'ultraguard-security-suite' ), 'value' => ! empty( $settings['uptime_monitor_recovery_email'] ) ],
                        [ 'type' => 'toggle', 'name' => 'uptime_monitor_weekly_report', 'label' => __( 'Send weekly uptime report', 'ultraguard-security-suite' ), 'value' => ! empty( $settings['uptime_monitor_weekly_report'] ) ],
                    ],
                ],
            ],
            'form'      => [
                'enable' => true,
                'id'     => 'uptime-monitor-settings-form',
                'ajax'   => true,
                'action' => 'ultraguard_save_uptime_monitor_settings',
                'nonce'  => $nonce,
                'submit' => [
                    'text'         => __( 'Save Settings', 'ultraguard-security-suite' ),
                    'button_style' => 'primary',
                    'icon'         => 'dashicons-saved',
                ],
            ],
        ];
    }
}
