<?php

namespace UltraguardPro\modules\upgrade_manager\pages;

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

class UpgradeManagerPaid extends UpgradeManagerPage {

    public function get_scheme( $context ): array {
        $status = $this->license_status();

        return [
            'type'      => 'page',
            'id'        => 'ultraguard-upgrade-manager-paid',
            'page_slug' => 'upgrade-manager-paid',
            'header'    => [
                'title'    => __( 'Subscription & Entitlements', 'ultraguard-security-suite' ),
                'subtitle' => __( 'Review the current subscription, refresh the license status, and move to Agency when you need more sites or a broader managed rollout.', 'ultraguard-security-suite' ),
                'badge'    => $this->status_badge( $status ),
                'actions'  => array_values( array_filter( [
                    $this->create_url_button( __( 'License Dashboard', 'ultraguard-security-suite' ), $this->get_page_url( 'upgrade-manager' ), 'secondary', 'dashicons-arrow-left-alt' ),
                    $this->create_ajax_button( __( 'Refresh Status', 'ultraguard-security-suite' ), 'ultraguard_upgrade_manager_refresh_status', [], 'primary', 'dashicons-update' ),
                    $status['manage_url'] ? $this->create_url_button( __( 'Manage Billing', 'ultraguard-security-suite' ), $status['manage_url'], 'secondary', 'dashicons-admin-links' ) : null,
                    $this->create_url_button( __( 'See Higher Plans', 'ultraguard-security-suite' ), $status['upgrade_url'], 'secondary', 'dashicons-external' ),
                ] ) ),
            ],
            'data'      => [
                ! $status['is_pro'] ? [
                    'type'       => 'alert-box',
                    'alert_type' => 'warning',
                    'title'      => __( 'No active paid subscription detected', 'ultraguard-security-suite' ),
                    'message'    => __( 'This page becomes your subscription dashboard once a paid license is activated. For now, use the Free Plan page to activate an existing key or compare plans.', 'ultraguard-security-suite' ),
                    'actions'    => [
                        [
                            'type'         => 'button',
                            'text'         => __( 'Open Free Plan Page', 'ultraguard-security-suite' ),
                            'button_style' => 'primary',
                            'url'          => $this->get_page_url( 'upgrade-manager-free' ),
                        ],
                    ],
                ] : [
                    'type'       => 'alert-box',
                    'alert_type' => 'success',
                    'title'      => __( 'Your subscription is active', 'ultraguard-security-suite' ),
                    'message'    => __( 'UltraGuard is currently operating with paid entitlements. The details below reflect the last confirmed local license state.', 'ultraguard-security-suite' ),
                ],
                [
                    'type'    => 'grid',
                    'columns' => 12,
                    'gap'     => '16px',
                    'items'   => [
                        [ 'span' => 3, 'responsive' => [ 'md' => 6, 'sm' => 12 ], 'component' => [ 'type' => 'stat-card', 'title' => __( 'Plan', 'ultraguard-security-suite' ), 'value' => $status['plan_label'], 'format' => 'string', 'icon' => 'dashicons-awards', 'class' => $status['is_pro'] ? 'success' : 'warning' ] ],
                        [ 'span' => 3, 'responsive' => [ 'md' => 6, 'sm' => 12 ], 'component' => [ 'type' => 'stat-card', 'title' => __( 'Status', 'ultraguard-security-suite' ), 'value' => ucfirst( $status['status'] ?: 'free' ), 'format' => 'string', 'icon' => 'dashicons-yes-alt', 'class' => $status['is_valid'] ? 'success' : 'warning' ] ],
                        [ 'span' => 3, 'responsive' => [ 'md' => 6, 'sm' => 12 ], 'component' => [ 'type' => 'stat-card', 'title' => __( 'Expiry', 'ultraguard-security-suite' ), 'value' => $status['expires_label'] ?: __( 'Not reported', 'ultraguard-security-suite' ), 'format' => 'string', 'icon' => 'dashicons-calendar-alt', 'class' => $status['expires_label'] ? 'info' : 'warning' ] ],
                        [ 'span' => 3, 'responsive' => [ 'md' => 6, 'sm' => 12 ], 'component' => [ 'type' => 'stat-card', 'title' => __( 'Included Modules', 'ultraguard-security-suite' ), 'value' => count( $this->current_plan_modules() ), 'format' => 'number', 'icon' => 'dashicons-admin-plugins', 'class' => 'success' ] ],
                    ],
                ],
                [
                    'type'    => 'grid',
                    'columns' => 12,
                    'gap'     => '20px',
                    'items'   => [
                        [
                            'span'       => 5,
                            'responsive' => [ 'lg' => 12 ],
                            'component'  => [
                                'type'           => 'card',
                                'title'          => __( 'Current Subscription', 'ultraguard-security-suite' ),
                                'icon'           => 'dashicons-admin-network',
                                'data'           => [
                                    [
                                        'type'       => 'data-table',
                                        'id'         => 'ultraguard-paid-plan-overview',
                                        'columns'    => [
                                            'item'  => [ 'title' => __( 'Item', 'ultraguard-security-suite' ) ],
                                            'value' => [ 'title' => __( 'Value', 'ultraguard-security-suite' ) ],
                                        ],
                                        'data'       => $this->plan_overview_rows( $status ),
                                        'searchable' => false,
                                        'pagination' => [ 'enabled' => false ],
                                    ],
                                ],
                                'footer_actions' => [
                                    $this->create_ajax_button( __( 'Deactivate License', 'ultraguard-security-suite' ), 'ultraguard_upgrade_manager_deactivate', [], 'danger', 'dashicons-dismiss', __( 'Deactivate this license on the current site and return to free mode?', 'ultraguard-security-suite' ) ),
                                ],
                            ],
                        ],
                        [
                            'span'       => 7,
                            'responsive' => [ 'lg' => 12 ],
                            'component'  => [
                                'type'  => 'card',
                                'title' => __( 'What Your Current Plan Unlocks', 'ultraguard-security-suite' ),
                                'icon'  => 'dashicons-shield-alt',
                                'data'  => [
                                    [
                                        'type'       => 'data-table',
                                        'id'         => 'ultraguard-paid-plan-modules',
                                        'columns'    => [
                                            'module'      => [ 'title' => __( 'Module', 'ultraguard-security-suite' ) ],
                                            'group'       => [ 'title' => __( 'Group', 'ultraguard-security-suite' ), 'width' => '130px' ],
                                            'description' => [ 'title' => __( 'Protection Added', 'ultraguard-security-suite' ) ],
                                        ],
                                        'data'       => array_map( static function ( array $row ): array {
                                            unset( $row['license'], $row['status'] );
                                            return $row;
                                        }, $this->build_module_rows( true ) ),
                                        'searchable' => true,
                                        'pagination' => [ 'enabled' => false ],
                                    ],
                                ],
                            ],
                        ],
                    ],
                ],
                [
                    'type'  => 'card',
                    'title' => __( 'Upgrade Path', 'ultraguard-security-suite' ),
                    'icon'  => 'dashicons-chart-line',
                    'data'  => [
                        [
                            'type'       => 'alert-box',
                            'alert_type' => 'info',
                            'title'      => __( 'Need more sites or a higher plan tier?', 'ultraguard-security-suite' ),
                            'message'    => __( 'Use the upgrade path below to move from your current subscription to a larger plan. UltraGuard keeps your current settings and license state while you move into a broader rollout.', 'ultraguard-security-suite' ),
                            'actions'    => array_values( array_filter( [
                                [
                                    'type'         => 'button',
                                    'text'         => __( 'View Higher Plans', 'ultraguard-security-suite' ),
                                    'button_style' => 'primary',
                                    'url'          => $status['upgrade_url'],
                                    'target'       => '_blank',
                                ],
                                $status['manage_url'] ? [
                                    'type'         => 'button',
                                    'text'         => __( 'Open Billing Portal', 'ultraguard-security-suite' ),
                                    'button_style' => 'secondary',
                                    'url'          => $status['manage_url'],
                                    'target'       => '_blank',
                                ] : null,
                            ] ) ),
                        ],
                    ],
                ],
            ],
            'form'      => [ 'enable' => false ],
        ];
    }
}
