<?php

namespace UltraguardPro\modules\auto_updater\pages;

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

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

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

        return [
            'type'      => 'page',
            'id'        => 'auto-updater-settings',
            'page_slug' => 'auto-updater-settings',
            'header'    => [
                'title'    => __( 'Auto Updater Settings', 'ultraguard-security-suite' ),
                'subtitle' => __( 'Control what updates run automatically, when they run, and what should never be updated automatically.', 'ultraguard-security-suite' ),
                'actions'  => [
                    $this->create_url_button( __( 'Back to Dashboard', 'ultraguard-security-suite' ), $this->get_page_url( 'auto-updater' ), 'secondary', 'dashicons-arrow-left-alt' ),
                ],
            ],
            'data'      => [
                [
                    'type'  => 'card',
                    'title' => __( 'Automatic Updates', 'ultraguard-security-suite' ),
                    'data'  => [
                        [ 'type' => 'toggle', 'name' => 'auto_updater_plugins_enabled', 'label' => __( 'Enable automatic plugin updates', 'ultraguard-security-suite' ), 'value' => ! empty( $settings['auto_updater_plugins_enabled'] ) ],
                        [ 'type' => 'toggle', 'name' => 'auto_updater_themes_enabled', 'label' => __( 'Enable automatic theme updates', 'ultraguard-security-suite' ), 'value' => ! empty( $settings['auto_updater_themes_enabled'] ) ],
                        [ 'type' => 'toggle', 'name' => 'auto_updater_core_minor_enabled', 'label' => __( 'Enable automatic WordPress core minor updates', 'ultraguard-security-suite' ), 'value' => ! empty( $settings['auto_updater_core_minor_enabled'] ) ],
                        [ 'type' => 'toggle', 'name' => 'auto_updater_core_major_enabled', 'label' => __( 'Enable automatic WordPress core major updates', 'ultraguard-security-suite' ), 'description' => __( 'Warning: major updates can introduce compatibility changes.', 'ultraguard-security-suite' ), 'value' => ! empty( $settings['auto_updater_core_major_enabled'] ) ],
                    ],
                ],
                [
                    'type'  => 'card',
                    'title' => __( 'Schedule & Policy', 'ultraguard-security-suite' ),
                    'data'  => [
                        [ 'type' => 'select', 'name' => 'auto_updater_schedule', 'label' => __( 'Auto-update schedule', 'ultraguard-security-suite' ), 'value' => (string) ( $settings['auto_updater_schedule'] ?? 'daily' ), 'options' => [
                            [ 'value' => 'disabled', 'label' => __( 'Disabled', 'ultraguard-security-suite' ) ],
                            [ 'value' => 'daily', 'label' => __( 'Daily', 'ultraguard-security-suite' ) ],
                            [ 'value' => 'weekly', 'label' => __( 'Weekly', 'ultraguard-security-suite' ) ],
                        ] ],
                        [ 'type' => 'text', 'name' => 'auto_updater_schedule_time', 'label' => __( 'Daily time (HH:MM)', 'ultraguard-security-suite' ), 'value' => (string) ( $settings['auto_updater_schedule_time'] ?? '02:00' ) ],
                        [ 'type' => 'select', 'name' => 'auto_updater_schedule_day', 'label' => __( 'Weekly day', 'ultraguard-security-suite' ), 'value' => (string) ( $settings['auto_updater_schedule_day'] ?? 'sunday' ), 'options' => [
                            [ 'value' => 'sunday', 'label' => __( 'Sunday', 'ultraguard-security-suite' ) ],
                            [ 'value' => 'monday', 'label' => __( 'Monday', 'ultraguard-security-suite' ) ],
                            [ 'value' => 'tuesday', 'label' => __( 'Tuesday', 'ultraguard-security-suite' ) ],
                            [ 'value' => 'wednesday', 'label' => __( 'Wednesday', 'ultraguard-security-suite' ) ],
                            [ 'value' => 'thursday', 'label' => __( 'Thursday', 'ultraguard-security-suite' ) ],
                            [ 'value' => 'friday', 'label' => __( 'Friday', 'ultraguard-security-suite' ) ],
                            [ 'value' => 'saturday', 'label' => __( 'Saturday', 'ultraguard-security-suite' ) ],
                        ] ],
                        [ 'type' => 'toggle', 'name' => 'auto_updater_security_only', 'label' => __( 'Only auto-update if Vulnerability Scanner detects active CVE', 'ultraguard-security-suite' ), 'value' => ! empty( $settings['auto_updater_security_only'] ) ],
                        [ 'type' => 'toggle', 'name' => 'auto_updater_maintenance_mode', 'label' => __( 'Maintenance mode during update', 'ultraguard-security-suite' ), 'value' => ! empty( $settings['auto_updater_maintenance_mode'] ) ],
                    ],
                ],
                [
                    'type'  => 'card',
                    'title' => __( 'Exclusions & Notifications', 'ultraguard-security-suite' ),
                    'data'  => [
                        [ 'type' => 'textarea', 'name' => 'auto_updater_excluded_plugins', 'label' => __( 'Excluded plugin slugs', 'ultraguard-security-suite' ), 'description' => __( 'One plugin slug per line.', 'ultraguard-security-suite' ), 'value' => (string) ( $settings['auto_updater_excluded_plugins'] ?? '' ) ],
                        [ 'type' => 'textarea', 'name' => 'auto_updater_excluded_themes', 'label' => __( 'Excluded theme slugs', 'ultraguard-security-suite' ), 'description' => __( 'One theme slug per line.', 'ultraguard-security-suite' ), 'value' => (string) ( $settings['auto_updater_excluded_themes'] ?? '' ) ],
                        [ 'type' => 'toggle', 'name' => 'auto_updater_email_summary', 'label' => __( 'Send email summary after batch auto-update run', 'ultraguard-security-suite' ), 'value' => ! empty( $settings['auto_updater_email_summary'] ) ],
                        [ 'type' => 'toggle', 'name' => 'auto_updater_alert_on_failure', 'label' => __( 'Send alert if any update fails', 'ultraguard-security-suite' ), 'value' => ! empty( $settings['auto_updater_alert_on_failure'] ) ],
                    ],
                ],
            ],
            'form'      => [
                'enable' => true,
                'id'     => 'auto-updater-settings-form',
                'ajax'   => true,
                'action' => 'ultraguard_save_auto_updater_settings',
                'nonce'  => $nonce,
                'submit' => [
                    'text'         => __( 'Save Settings', 'ultraguard-security-suite' ),
                    'button_style' => 'primary',
                    'icon'         => 'dashicons-saved',
                ],
            ],
        ];
    }
}
