<?php
/**
 * UpgradePage — The dedicated Pro upgrade comparison page.
 *
 * Shown via `?ug_page=upgrade`. Visible to ALL users (free and pro alike).
 * For pro users it becomes a "Your Plan" summary page instead.
 *
 * Layout (free users):
 *   1. Header  — "You're on the Free plan" + security score with gap
 *   2. Comparison table — feature-by-feature Free vs Pro
 *   3. Why Pro cards — 3 benefit cards (speed, intelligence, peace of mind)
 *   4. Testimonials panel — social proof (generic, no real names needed)
 *   5. FAQ accordion
 *   6. Upgrade CTA card — prominent button linking to the website
 *
 * Layout (pro users):
 *   1. Header — "You're on Pro" celebration
 *   2. What you have — current plan feature list
 *   3. What's coming — roadmap teaser
 *
 * @package UltraguardPro\support
 * @since   6.7.0
 */

namespace UltraguardPro\support;

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

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

class UpgradePage extends BasePage {

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

    public function get_scheme( $context ): array {
        $is_pro = LicenseInfo::instance()->isPro();

        return $is_pro
            ? $this->pro_plan_scheme()
            : $this->free_upgrade_scheme();
    }

    // =========================================================================
    // FREE USER VIEW
    // =========================================================================

    private function free_upgrade_scheme(): array {
        return [
            'type'      => 'page',
            'id'        => 'ultraguard-upgrade',
            'page_slug' => 'upgrade',

            'header' => [
                'title'    => __( 'Upgrade to UltraGuard Pro', 'ultraguard-security-suite' ),
                'subtitle' => __( 'You\'re on the Free plan. Everything below shows you exactly what you\'ll unlock — no surprises, no pressure. Prices are set on our website when you\'re ready.', 'ultraguard-security-suite' ),
                'badge'    => [
                    'text'       => __( 'Free Plan', 'ultraguard-security-suite' ),
                    'badge_type' => 'warning',
                ],
                'actions' => [
                    [
                        'type'         => 'button',
                        'text'         => __( 'See Pricing on UltraGuard.in →', 'ultraguard-security-suite' ),
                        'button_style' => 'primary',
                        'icon'         => 'dashicons-external',
                        'url'          => UpgradeManager::EXTERNAL_UPGRADE_URL,
                        'target'       => '_blank',
                    ],
                ],
            ],

            'data' => [

                // ── What you're missing right now (top alert) ─────────────────
                [
                    'type'       => 'alert-box',
                    'alert_type' => 'warning',
                    'title'      => __( '24-hour intelligence update window on the Free plan', 'ultraguard-security-suite' ),
                    'message'    => __( 'Your malware signatures, threat IP blacklist, firewall rules, and vulnerability database are delivered within 24 hours of publication. Pro users get updates the instant they\'re released — zero delay.', 'ultraguard-security-suite' ),
                    'actions'    => [ [
                        'type'         => 'button',
                        'text'         => __( 'Fix This — Upgrade to Pro', 'ultraguard-security-suite' ),
                        'button_style' => 'primary',
                        'url'          => UpgradeManager::EXTERNAL_UPGRADE_URL,
                        'target'       => '_blank',
                    ] ],
                ],

                // ── Feature comparison table ──────────────────────────────────
                [
                    'type'  => 'card',
                    'title' => __( 'Free vs Pro — Full Comparison', 'ultraguard-security-suite' ),
                    'icon'  => 'dashicons-list-view',
                    'data'  => [ $this->comparison_table() ],
                ],

                // ── Benefit highlight cards ───────────────────────────────────
                [
                    'type'    => 'grid',
                    'columns' => 12,
                    'gap'     => '20px',
                    'items'   => [
                        [
                            'span' => 4, 'responsive' => [ 'md' => 6, 'sm' => 12 ],
                            'component' => $this->benefit_card(
                                __( '⚡ Zero-Day Protection', 'ultraguard-security-suite' ),
                                'dashicons-shield',
                                __( 'Threat signatures and firewall rules pushed to your site instantly for Pro users — free users receive updates within 24 hours.', 'ultraguard-security-suite' ),
                                [ __( 'Hourly threat IP blacklist', 'ultraguard-security-suite' ), __( 'Instant signature updates', 'ultraguard-security-suite' ), __( 'Cloud WAF rules on day 0', 'ultraguard-security-suite' ) ]
                            ),
                        ],
                        [
                            'span' => 4, 'responsive' => [ 'md' => 6, 'sm' => 12 ],
                            'component' => $this->benefit_card(
                                __( '🔍 File Integrity Monitoring', 'ultraguard-security-suite' ),
                                'dashicons-search',
                                __( 'Know the moment any file on your server changes — WordPress core, plugins, themes, or your own code.', 'ultraguard-security-suite' ),
                                [ __( 'Scheduled automatic scans', 'ultraguard-security-suite' ), __( 'Core file checksum verification', 'ultraguard-security-suite' ), __( 'Safe file quarantine', 'ultraguard-security-suite' ) ]
                            ),
                        ],
                        [
                            'span' => 4, 'responsive' => [ 'md' => 6, 'sm' => 12 ],
                            'component' => $this->benefit_card(
                                __( '🔔 Smart Alerts', 'ultraguard-security-suite' ),
                                'dashicons-bell',
                                __( 'Custom notification rules per event type, with Slack, webhook, and email delivery — so you\'re never flooded with noise.', 'ultraguard-security-suite' ),
                                [ __( 'Slack, webhook, and email channels', 'ultraguard-security-suite' ), __( 'Per-event severity thresholds', 'ultraguard-security-suite' ), __( 'Cooldown periods to avoid fatigue', 'ultraguard-security-suite' ) ]
                            ),
                        ],
                    ],
                ],

                // ── FAQ ───────────────────────────────────────────────────────
                [
                    'type'  => 'card',
                    'title' => __( 'Frequently Asked Questions', 'ultraguard-security-suite' ),
                    'icon'  => 'dashicons-editor-help',
                    'data'  => [ $this->faq_list() ],
                ],

                // ── Bottom CTA ────────────────────────────────────────────────
                $this->bottom_cta(),
            ],
        ];
    }

    // =========================================================================
    // PRO USER VIEW
    // =========================================================================

    private function pro_plan_scheme(): array {
        $status = LicenseInfo::instance()->getStatus();

        return [
            'type'      => 'page',
            'id'        => 'ultraguard-upgrade',
            'page_slug' => 'upgrade',

            'header' => [
                'title'    => __( 'You\'re on UltraGuard Pro 🎉', 'ultraguard-security-suite' ),
                'subtitle' => __( 'You have access to every feature UltraGuard offers. Here\'s a summary of what\'s protecting your site right now.', 'ultraguard-security-suite' ),
                'badge'    => [
                    'text'       => __( 'Pro Plan', 'ultraguard-security-suite' ),
                    'badge_type' => 'success',
                ],
            ],

            'data' => [
                [
                    'type'       => 'alert-box',
                    'alert_type' => 'success',
                    'title'      => __( 'All Pro features active', 'ultraguard-security-suite' ),
                    'message'    => __( 'Threat intelligence syncs in real-time. File integrity monitoring is active. Notifications deliver to your configured channels. You\'re fully protected.', 'ultraguard-security-suite' ),
                ],
                [
                    'type'  => 'card',
                    'title' => __( 'Your Pro Features', 'ultraguard-security-suite' ),
                    'icon'  => 'dashicons-awards',
                    'data'  => [ [
                        'type'  => 'list',
                        'items' => [
                            [ 'icon' => 'dashicons-yes', 'text' => __( 'Real-time threat intelligence (0-day)', 'ultraguard-security-suite' ) ],
                            [ 'icon' => 'dashicons-yes', 'text' => __( 'Hourly cloud threat IP blacklist', 'ultraguard-security-suite' ) ],
                            [ 'icon' => 'dashicons-yes', 'text' => __( 'File integrity scanning and quarantine', 'ultraguard-security-suite' ) ],
                            [ 'icon' => 'dashicons-yes', 'text' => __( 'Custom notification rules (Slack, webhook, email)', 'ultraguard-security-suite' ) ],
                            [ 'icon' => 'dashicons-yes', 'text' => __( 'Cloud firewall rules pushed instantly', 'ultraguard-security-suite' ) ],
                            [ 'icon' => 'dashicons-yes', 'text' => __( '.htaccess security manager', 'ultraguard-security-suite' ) ],
                            [ 'icon' => 'dashicons-yes', 'text' => __( 'Third-party module upload', 'ultraguard-security-suite' ) ],
                            [ 'icon' => 'dashicons-yes', 'text' => __( 'Priority support', 'ultraguard-security-suite' ) ],
                        ],
                    ] ],
                ],
                [
                    'type'       => 'alert-box',
                    'alert_type' => 'info',
                    'title'      => __( 'Coming next', 'ultraguard-security-suite' ),
                    'message'    => __( 'We\'re building: geo-fencing rules, two-factor authentication management, WP-CLI integration, and a REST API for programmatic security events. Pro users get every new feature the day it ships.', 'ultraguard-security-suite' ),
                ],
            ],
        ];
    }

    // =========================================================================
    // COMPONENT BUILDERS
    // =========================================================================

    /**
     * Build the Free vs Pro comparison data-table.
     */
    private function comparison_table(): array {
        $check   = '<span style="color:#10b981;font-weight:600">✓</span>';
        $cross   = '<span style="color:#94a3b8">—</span>';
        $delayed = '<span style="color:#f59e0b">✓ (within 24h)</span>';

        return [
            'type'        => 'data-table',
            'id'          => 'ug-comparison-table',
            'columns'     => [
                'feature'  => [ 'title' => __( 'Feature', 'ultraguard-security-suite' ), 'width' => '55%' ],
                'free'     => [ 'title' => __( '🆓 Free', 'ultraguard-security-suite' ),  'width' => '22%' ],
                'pro'      => [ 'title' => __( '⭐ Pro',   'ultraguard-security-suite' ),  'width' => '23%' ],
            ],
            'data'        => [
                // Core protection
                [ 'feature' => '<strong>' . __( 'Core Protection', 'ultraguard-security-suite' ) . '</strong>',                        'free' => '',         'pro' => ''       ],
                [ 'feature' => __( 'WordPress Firewall (WAF)',             'ultraguard-security-suite' ),                               'free' => $check,     'pro' => $check   ],
                [ 'feature' => __( 'Brute Force / Login Protection',       'ultraguard-security-suite' ),                               'free' => $check,     'pro' => $check   ],
                [ 'feature' => __( 'Real-time Security Event Push',        'ultraguard-security-suite' ),                               'free' => $check,     'pro' => $check   ],
                [ 'feature' => __( 'Security Dashboard & Activity Log',    'ultraguard-security-suite' ),                               'free' => $check,     'pro' => $check   ],
                [ 'feature' => __( 'Basic Email Notifications',            'ultraguard-security-suite' ),                               'free' => $check,     'pro' => $check   ],

                // Threat Intelligence
                [ 'feature' => '<strong>' . __( 'Threat Intelligence Updates', 'ultraguard-security-suite' ) . '</strong>',            'free' => '',         'pro' => ''       ],
                [ 'feature' => __( 'Cloud Threat IP Blacklist',            'ultraguard-security-suite' ),                               'free' => $delayed,   'pro' => $check . ' (Hourly)' ],
                [ 'feature' => __( 'Malware Signature Database',           'ultraguard-security-suite' ),                               'free' => $delayed,   'pro' => $check . ' (Instant)'  ],
                [ 'feature' => __( 'Vulnerability Database (CVE)',         'ultraguard-security-suite' ),                               'free' => $delayed,   'pro' => $check . ' (Instant)'  ],
                [ 'feature' => __( 'Cloud Firewall Rules',                 'ultraguard-security-suite' ),                               'free' => $delayed,   'pro' => $check . ' (Instant)'  ],
                [ 'feature' => __( '.htaccess Rule Templates',             'ultraguard-security-suite' ),                               'free' => $delayed,   'pro' => $check . ' (Instant)'  ],

                // File Integrity
                [ 'feature' => '<strong>' . __( 'File Integrity Monitoring', 'ultraguard-security-suite' ) . '</strong>',             'free' => '',         'pro' => ''       ],
                [ 'feature' => __( 'File Integrity Scanning',             'ultraguard-security-suite' ),                               'free' => $cross,     'pro' => $check   ],
                [ 'feature' => __( 'WordPress Core Checksum Verification', 'ultraguard-security-suite' ),                              'free' => $cross,     'pro' => $check   ],
                [ 'feature' => __( 'Scheduled Automatic Scans',           'ultraguard-security-suite' ),                               'free' => $cross,     'pro' => $check   ],
                [ 'feature' => __( 'File Quarantine & Restore',           'ultraguard-security-suite' ),                               'free' => $cross,     'pro' => $check   ],
                [ 'feature' => __( 'Baseline Reset After Updates',        'ultraguard-security-suite' ),                               'free' => $cross,     'pro' => $check   ],
                [ 'feature' => __( 'Malware Pattern Scanning',            'ultraguard-security-suite' ),                               'free' => $cross,     'pro' => $check   ],

                // Advanced Notifications
                [ 'feature' => '<strong>' . __( 'Advanced Notifications', 'ultraguard-security-suite' ) . '</strong>',                'free' => '',         'pro' => ''       ],
                [ 'feature' => __( 'Custom Notification Rules',           'ultraguard-security-suite' ),                               'free' => $cross,     'pro' => $check   ],
                [ 'feature' => __( 'Slack & Webhook Delivery',            'ultraguard-security-suite' ),                               'free' => $cross,     'pro' => $check   ],
                [ 'feature' => __( 'Per-Event Severity Thresholds',       'ultraguard-security-suite' ),                               'free' => $cross,     'pro' => $check   ],
                [ 'feature' => __( 'Alert Cooldown Periods',              'ultraguard-security-suite' ),                               'free' => $cross,     'pro' => $check   ],
                [ 'feature' => __( 'Notification Delivery Log',           'ultraguard-security-suite' ),                               'free' => $cross,     'pro' => $check   ],

                // Extensibility
                [ 'feature' => '<strong>' . __( 'Extensibility', 'ultraguard-security-suite' ) . '</strong>',                         'free' => '',         'pro' => ''       ],
                [ 'feature' => __( 'Third-Party Module Upload (ZIP)',     'ultraguard-security-suite' ),                               'free' => $cross,     'pro' => $check   ],
                [ 'feature' => __( '.htaccess Manager',                   'ultraguard-security-suite' ),                               'free' => $cross,     'pro' => $check   ],
                [ 'feature' => __( 'Priority Support',                    'ultraguard-security-suite' ),                               'free' => $cross,     'pro' => $check   ],
            ],
            'searchable'  => false,
            'pagination'  => [ 'enabled' => false ],
            'empty_state' => [ 'title' => '', 'message' => '' ],
        ];
    }

    /**
     * Build a single "Why Pro" benefit card.
     */
    private function benefit_card( string $title, string $icon, string $desc, array $bullets ): array {
        $items = array_map( fn( $b ) => [ 'icon' => 'dashicons-yes', 'text' => $b ], $bullets );

        return [
            'type'  => 'card',
            'title' => $title,
            'icon'  => $icon,
            'data'  => [
                [
                    'type'       => 'alert-box',
                    'alert_type' => 'info',
                    'title'      => '',
                    'message'    => $desc,
                ],
                [ 'type' => 'list', 'items' => $items ],
            ],
        ];
    }

    /**
     * Build the FAQ list component.
     */
    private function faq_list(): array {
        $faqs = [
            [
                'q' => __( 'Do I lose any security protection on the Free plan?', 'ultraguard-security-suite' ),
                'a' => __( 'Your core firewall, brute-force protection, and security monitoring all work on the Free plan. The gap is in threat intelligence freshness: signatures, IP blacklists, and firewall rule updates arrive within 24 hours (vs. instantly for Pro users). For most sites this is acceptable. For production ecommerce or sites handling sensitive data, Pro is recommended.', 'ultraguard-security-suite' ),
            ],
            [
                'q' => __( 'What\'s the pricing?', 'ultraguard-security-suite' ),
                'a' => __( 'UltraGuard Pro is $149 per year for one site. The Agency plan is $399 per year for up to 20 sites. Use the pricing button to subscribe and activate your license.', 'ultraguard-security-suite' ),
            ],
            [
                'q' => __( 'Can I upgrade later without losing my settings?', 'ultraguard-security-suite' ),
                'a' => __( 'Yes. Upgrading activates Pro features on top of your existing configuration. All your current rules, logs, and settings carry over. You can also downgrade back to Free at any time — Pro-only data is retained but the Pro-only interfaces are hidden until you upgrade again.', 'ultraguard-security-suite' ),
            ],
            [
                'q' => __( 'Is the File Integrity scanner available on Free?', 'ultraguard-security-suite' ),
                'a' => __( 'The file integrity scanner — including scheduled scans, core checksum verification, malware pattern detection, and file quarantine — is a Pro feature. Free users can see the scanner dashboard to understand what it does, but running scans and taking action on results requires Pro.', 'ultraguard-security-suite' ),
            ],
            [
                'q' => __( 'What happens if my Pro license expires?', 'ultraguard-security-suite' ),
                'a' => __( 'Your site stays protected by the Free plan features. Intelligence updates shift back to the 24-hour delivery window. File integrity scanning pauses. Notification rules are retained but not dispatched until you renew.', 'ultraguard-security-suite' ),
            ],
            [
                'q' => __( 'Do I need technical knowledge to use Pro features?', 'ultraguard-security-suite' ),
                'a' => __( 'No. Every Pro feature has a guided setup inside the UltraGuard dashboard. The file scanner, notification rules, and intelligence sync all work out of the box with sensible defaults.', 'ultraguard-security-suite' ),
            ],
        ];

        $items = array_map( fn( $faq ) => [
            'icon'  => 'dashicons-editor-help',
            'text'  => '<strong>' . esc_html( $faq['q'] ) . '</strong><br><span style="color:#64748b;font-size:13px;">' . esc_html( $faq['a'] ) . '</span>',
        ], $faqs );

        return [ 'type' => 'list', 'items' => $items ];
    }

    /**
     * Build the bottom CTA card.
     */
    private function bottom_cta(): array {
        return [
            'type'  => 'card',
            'title' => __( 'Ready to upgrade?', 'ultraguard-security-suite' ),
            'icon'  => 'dashicons-awards',
            'data'  => [
                [
                    'type'       => 'alert-box',
                    'alert_type' => 'success',
                    'title'      => __( 'Pro: $149 / year. Agency: $399 / year.', 'ultraguard-security-suite' ),
                    'message'    => __( 'Choose the plan that fits your rollout. Activation is instant — enter your license key in the Settings → License page and premium features unlock immediately.', 'ultraguard-security-suite' ),
                    'actions'    => [
                        [
                            'type'         => 'button',
                            'text'         => __( 'See Pricing & Subscribe →', 'ultraguard-security-suite' ),
                            'button_style' => 'primary',
                            'icon'         => 'dashicons-external',
                            'url'          => UpgradeManager::EXTERNAL_UPGRADE_URL,
                            'target'       => '_blank',
                        ],
                        [
                            'type'         => 'button',
                            'text'         => __( 'Activate Existing License', 'ultraguard-security-suite' ),
                            'button_style' => 'secondary',
                            'icon'         => 'dashicons-admin-network',
                            'url'          => self_admin_url( 'admin.php?page=ultraguard-security-suite-dashboard&ug_page=settings' ),
                        ],
                    ],
                ],
            ],
        ];
    }
}
