<?php

namespace UltraguardPro\modules\server_intelligence\pages;

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

use UltraguardPro\support\LicenseInfo;
use UltraguardPro\support\ServerCommunication;

class ServerIntelligenceSettings extends ServerIntelligencePage {

    public function get_scheme( $context ): array {
        $is_pro      = LicenseInfo::instance()->isPro();
        $free_delay  = LicenseInfo::instance()->getUpdateDelay();
        $last_pull   = (int) get_option( 'ultraguard_si_last_pull', 0 );
        $sync_config = $this->server_communication()->get_schedule_snapshot();
        $nonce       = $this->create_nonce( 'ultraguard_admin_nonce' );

        return [
            'type'      => 'page',
            'id'        => 'ultraguard-server-intelligence-settings',
            'page_slug' => 'server-intelligence-settings',
            'header'    => [
                'title'    => __( 'Server Intelligence Settings', 'ultraguard-security-suite' ),
                'subtitle' => __( 'Configure automatic cloud fetches, choose how often each feed runs, and manage testing actions from one dedicated settings page.', 'ultraguard-security-suite' ),
                'actions'  => [
                    $this->create_url_button( __( 'Back to Intelligence', 'ultraguard-security-suite' ), $this->get_page_url( 'server-intelligence' ), 'secondary', 'dashicons-arrow-left-alt' ),
                    $this->create_ajax_button( __( 'Pull All Now', 'ultraguard-security-suite' ), 'ultraguard_si_pull_all', [], 'primary', 'dashicons-update', __( 'Force-sync all intelligence sources from the server?', 'ultraguard-security-suite' ) ),
                ],
            ],
            'data'      => [
                [
                    'type'       => 'alert-box',
                    'alert_type' => $is_pro ? 'success' : 'warning',
                    'title'      => $is_pro ? __( 'Pro sync active', 'ultraguard-security-suite' ) : __( 'Free tier: updates within 24 hours', 'ultraguard-security-suite' ),
                    'message'    => $last_pull
                        /* translators: %s: string */
                        ? sprintf( __( 'Last full intelligence pull: %s ago.', 'ultraguard-security-suite' ), human_time_diff( $last_pull ) )
                        : __( 'No intelligence pull has completed yet.', 'ultraguard-security-suite' ),
                ],
                [
                    'type'  => 'card',
                    'title' => __( 'Automatic Fetch Controls', 'ultraguard-security-suite' ),
                    'icon'  => 'dashicons-controls-repeat',
                    'data'  => array_merge(
                        [
                            [
                                'type'       => 'alert-box',
                                'alert_type' => 'info',
                                'title'      => __( 'Granular sync timing', 'ultraguard-security-suite' ),
                                'message'    => $is_pro
                                    ? __( 'Each intelligence feed can be enabled and scheduled independently. Faster intervals mean fresher data but more frequent remote checks.', 'ultraguard-security-suite' )
                                    : __( 'You can tune how often each feed checks for updates, but free-tier updates arrive within 24 hours of publication.', 'ultraguard-security-suite' ),
                            ],
                        ],
                        $this->build_sync_control_fields( $sync_config )
                    ),
                ],
                [
                    'type'  => 'card',
                    'title' => __( 'Current Schedule Snapshot', 'ultraguard-security-suite' ),
                    'icon'  => 'dashicons-calendar-alt',
                    'data'  => [
                        [
                            'type'       => 'data-table',
                            'id'         => 'server-intelligence-settings-schedule-table',
                            'columns'    => [
                                'resource' => [ 'title' => __( 'Resource', 'ultraguard-security-suite' ) ],
                                'enabled'  => [ 'title' => __( 'Auto Fetch', 'ultraguard-security-suite' ) ],
                                'interval' => [ 'title' => __( 'Frequency', 'ultraguard-security-suite' ) ],
                                'next_run' => [ 'title' => __( 'Next Run', 'ultraguard-security-suite' ) ],
                                'status'   => [ 'title' => __( 'Status', 'ultraguard-security-suite' ) ],
                                'sync_now' => [ 'title' => __( 'Action', 'ultraguard-security-suite' ) ],
                            ],
                            'data'       => $this->build_schedule_rows( $nonce, $sync_config ),
                            'searchable' => false,
                            'pagination' => [ 'enabled' => false ],
                        ],
                    ],
                ],
            ],
            'form'      => [
                'enable' => true,
                'id'     => 'server-intelligence-settings-form',
                'ajax'   => true,
                'action' => 'ultraguard_si_save_settings',
                'nonce'  => $nonce,
                'submit' => [
                    'text'         => __( 'Save Sync Settings', 'ultraguard-security-suite' ),
                    'button_style' => 'primary',
                    'icon'         => 'dashicons-saved',
                ],
            ],
        ];
    }
}
