<?php
/**
 * DashboardSettings — Global settings page (consolidated view).
 *
 * Provides a single-page tabbed settings overview for all UltraGuard modules.
 * Each module's tab content is populated via the `ultraguard_settings_tabs` filter,
 * allowing modules to inject their own tab without this page knowing about them.
 *
 * @package UltraguardPro\modules\dashboard\pages
 */

namespace UltraguardPro\modules\dashboard\pages;

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

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

class DashboardSettings extends BasePage {

    public function __construct( Plugin $plugin ) {
        parent::__construct( $plugin );
    }

    /**
     * Build the settings page schema.
     *
     * @param  mixed $context  Page render context.
     * @return array<string,mixed>
     */
    public function get_scheme( $context ): array {
        $nonce = $this->create_nonce( 'ultraguard_admin_nonce' );

        return [
            'type'      => 'page',
            'id'        => 'ultraguard-settings',
            'page_slug' => 'settings',
            'class'     => 'ultraguard-settings-page',
            'header'    => [
                'id'       => 'settings-header',
                'title'    => __( 'UltraGuard Pro Settings', 'ultraguard-security-suite' ),
                'subtitle' => __( 'Global configuration options for the UltraGuard Pro plugin.', 'ultraguard-security-suite' ),
                'icon'     => 'dashicons-admin-settings',
            ],
            'data' => [ [
                'type' => 'tabs',
                'id'   => 'settings-tabs',
                'tabs' => apply_filters( 'ultraguard_settings_tabs', $this->get_default_tabs() ),
            ] ],
            'form' => [
                'enable' => true,
                'id'     => 'dashboard-settings-form',
                'ajax'   => true,
                'action' => 'ultraguard_save_consolidated_settings',
                'nonce'  => $nonce,
                'submit' => [
                    'text'         => __( 'Save Settings', 'ultraguard-security-suite' ),
                    'button_style' => 'primary',
                ],
            ],
        ];
    }

    // ── Private helpers ───────────────────────────────────────────────────────

    /**
     * Build the default tab definitions. Module-specific tabs are added via
     * the `ultraguard_settings_tabs` filter.
     *
     * @return array<int,array<string,mixed>>
     */
    private function get_default_tabs(): array {
        return [
            [
                'id'    => 'general',
                'title' => __( 'General', 'ultraguard-security-suite' ),
                'icon'  => 'dashicons-admin-generic',
                'data'  => $this->build_general_tab(),
            ],
            [
                'id'    => 'advanced',
                'title' => __( 'Advanced', 'ultraguard-security-suite' ),
                'icon'  => 'dashicons-admin-tools',
                'data'  => $this->build_advanced_tab(),
            ],
        ];
    }

    /**
     * General settings tab content.
     *
     * @return array<int,array<string,mixed>>
     */
    private function build_general_tab(): array {
        $s = fn( string $k, $d = '' ) => $this->get_setting( $k, $d );

        return [ [
            'type'  => 'grid',
            'columns' => 12,
            'gap'     => '20px',
            'items' => [
                [ 'span' => 6, 'component' => [
                    'type'  => 'card',
                    'title' => __( 'Plugin Status', 'ultraguard-security-suite' ),
                    'icon'  => 'dashicons-shield-alt',
                    'data'  => [
                        [ 'type' => 'toggle', 'name' => 'plugin_enabled', 'label' => __( 'Enable UltraGuard Pro', 'ultraguard-security-suite' ), 'description' => __( 'Master switch. Disabling this deactivates all security modules.', 'ultraguard-security-suite' ), 'default' => (bool) $s( 'plugin_enabled', true ) ],
                    ],
                ] ],
                [ 'span' => 6, 'component' => [
                    'type'  => 'card',
                    'title' => __( 'Contact & Notifications', 'ultraguard-security-suite' ),
                    'icon'  => 'dashicons-email',
                    'data'  => [
                        [ 'type' => 'text',   'name' => 'admin_email',              'label' => __( 'Admin Email',           'ultraguard-security-suite' ), 'placeholder' => get_option( 'admin_email' ), 'description' => __( 'Used as the fallback for all security alerts.', 'ultraguard-security-suite' ), 'default' => $s( 'admin_email', get_option( 'admin_email' ) ) ],
                        [ 'type' => 'toggle', 'name' => 'is_email_notifications',   'label' => __( 'Global Email Alerts',   'ultraguard-security-suite' ), 'description' => __( 'Allow modules to send email notifications.', 'ultraguard-security-suite' ), 'default' => (bool) $s( 'is_email_notifications', false ) ],
                    ],
                ] ],
            ],
        ] ];
    }

    /**
     * Advanced settings tab content.
     *
     * @return array<int,array<string,mixed>>
     */
    private function build_advanced_tab(): array {
        return [ [
            'type'  => 'grid',
            'columns' => 12,
            'gap'     => '20px',
            'items' => [
                [ 'span' => 12, 'component' => [
                    'type'  => 'card',
                    'title' => __( 'Data Management', 'ultraguard-security-suite' ),
                    'icon'  => 'dashicons-database',
                    'data'  => [
                        [
                            'type'    => 'alert-box',
                            'alert_type' => 'warning',
                            'message' => __( 'Resetting settings or exporting data affects all security modules. Use caution.', 'ultraguard-security-suite' ),
                        ],
                    ],
                    'footer' => [
                        'type'      => 'button-group',
                        'alignment' => 'left',
                        'buttons'   => [
                            [
                                'type'         => 'button',
                                'text'         => __( 'Export All Settings', 'ultraguard-security-suite' ),
                                'button_style' => 'secondary',
                                'icon'         => 'dashicons-download',
                                'ajax'         => [
                                    'action' => 'ultraguard_export_all_settings',
                                    'nonce'  => $this->create_nonce( 'ultraguard_admin_nonce' ),
                                ],
                            ],
                            [
                                'type'         => 'button',
                                'text'         => __( 'Reset to Defaults', 'ultraguard-security-suite' ),
                                'button_style' => 'danger',
                                'icon'         => 'dashicons-trash',
                                'ajax'         => [
                                    'action'  => 'ultraguard_reset_all_settings',
                                    'nonce'   => $this->create_nonce( 'ultraguard_admin_nonce' ),
                                    'confirm' => __( 'Are you sure you want to reset all settings to their defaults? This cannot be undone.', 'ultraguard-security-suite' ),
                                ],
                            ],
                        ],
                    ],
                ] ],
            ],
        ] ];
    }
}
