<?php

namespace UltraguardPro\modules\upgrade_manager\pages;

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

class UpgradeManagerFree extends UpgradeManagerPage {

    public function get_scheme( $context ): array {
        $status         = $this->license_status();
        $plan_key       = $this->current_plan_key( $status );
        $locked_modules = $this->free_locked_modules();
        $nonce          = $this->create_nonce( 'ultraguard_admin_nonce' );
        $is_pro_upsell  = 'pro' === $plan_key && $status['is_valid'];

        $promo_grid = $is_pro_upsell
            ? [
                'type'    => 'grid',
                'columns' => 12,
                'gap'     => '20px',
                'items'   => $this->agency_upsell_cards(),
            ]
            : [
                'type'    => 'grid',
                'columns' => 12,
                'gap'     => '20px',
                'items'   => $this->benefit_cards( $locked_modules, __( 'Unlock With Paid Plan', 'ultraguard-security-suite' ) ),
            ];

        $promo_detail_card = $is_pro_upsell
            ? $this->agency_upsell_summary_card()
            : [
                'type'  => 'card',
                'title' => __( 'Locked Premium Modules', 'ultraguard-security-suite' ),
                'icon'  => 'dashicons-lock',
                'data'  => [
                    [
                        'type'       => 'data-table',
                        'id'         => 'ultraguard-upgrade-manager-free-modules',
                        'columns'    => [
                            'module'      => [ 'title' => __( 'Module', 'ultraguard-security-suite' ) ],
                            'group'       => [ 'title' => __( 'Group', 'ultraguard-security-suite' ), 'width' => '130px' ],
                            'description' => [ 'title' => __( 'Benefit You Unlock', '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 ],
                    ],
                ],
            ];

        return [
            'type'      => 'page',
            'id'        => 'ultraguard-upgrade-manager-free',
            'page_slug' => 'upgrade-manager-free',
            'header'    => [
                'title'    => __( 'Plan & License Activation', 'ultraguard-security-suite' ),
                'subtitle' => __( 'Understand the Free plan, see what paid tiers unlock, and activate an existing license from one place.', 'ultraguard-security-suite' ),
                'badge'    => $this->status_badge( $status ),
                'actions'  => [
                    $this->create_url_button( __( 'License Dashboard', 'ultraguard-security-suite' ), $this->get_page_url( 'upgrade-manager' ), 'secondary', 'dashicons-awards' ),
                    $this->create_ajax_button( __( 'Refresh Status', 'ultraguard-security-suite' ), 'ultraguard_upgrade_manager_refresh_status', [], 'secondary', 'dashicons-update' ),
                    $this->create_url_button( __( 'View Plans', 'ultraguard-security-suite' ), $status['upgrade_url'], 'primary', 'dashicons-external' ),
                ],
            ],
            'data'      => [
                [
                    'type'       => 'alert-box',
                    'alert_type' => $status['is_pro'] ? 'success' : 'warning',
                    'title'      => $status['is_pro']
                        ? ( 'agency' === $plan_key
                            ? __( 'Your Agency license is already active on this site', 'ultraguard-security-suite' )
                            : __( 'Your Pro license is active. Scale it with Agency when you are ready.', 'ultraguard-security-suite' ) )
                        /* translators: %d: integer */
                        : sprintf( __( 'Free plan: %d advanced modules are still locked', 'ultraguard-security-suite' ), count( $locked_modules ) ),
                    'message'    => $status['is_pro']
                        ? ( 'agency' === $plan_key
                            ? sprintf(
                                /* translators: %s: active plan label */
                                __( 'This page still lets you validate or replace a key later, but the current site is already covered by the %s plan and has the broadest rollout entitlement available today.', 'ultraguard-security-suite' ),
                                $status['plan_label']
                            )
                            : __( 'Your current Pro plan already unlocks the premium stack on this site. The content below now focuses on why Agency is the natural next step when you want the same protection across more installs.', 'ultraguard-security-suite' ) )
                        : __( 'Free protection remains active, but advanced monitoring, reporting, reputation, uptime, WooCommerce security, and premium intelligence modules require a paid plan. Activating a license here validates it and unlocks the paid tier immediately.', 'ultraguard-security-suite' ),
                ],
                [
                    'type'   => 'card',
                    'title'  => $status['is_pro']
                        ? __( 'Activate Replacement License', 'ultraguard-security-suite' )
                        : __( 'Activate Existing License', 'ultraguard-security-suite' ),
                    'icon'   => 'dashicons-admin-network',
                    'data'   => [
                        [
                            'type'        => 'text',
                            'name'        => 'license_key',
                            'label'       => __( 'License key', 'ultraguard-security-suite' ),
                            'value'       => '',
                            'placeholder' => __( 'UGP-XXXX-XXXX-XXXX', 'ultraguard-security-suite' ),
                            'help_text'   => __( 'Paste the license key exactly as provided in your UltraGuard account.', 'ultraguard-security-suite' ),
                        ],
                        [
                            'type'        => 'text',
                            'name'        => 'license_email',
                            'label'       => __( 'License email', 'ultraguard-security-suite' ),
                            'value'       => $status['email'],
                            'placeholder' => __( 'name@example.com', 'ultraguard-security-suite' ),
                            'help_text'   => __( 'Use the email address associated with the subscription so activation can be validated correctly.', 'ultraguard-security-suite' ),
                        ],
                    ],
                    'footer' => [
                        'type'      => 'button-group',
                        'alignment' => 'left',
                        'buttons'   => [
                            [
                                'type'         => 'submit',
                                'text'         => $status['is_pro']
                                    ? __( 'Validate Replacement License', 'ultraguard-security-suite' )
                                    : __( 'Activate & Validate License', 'ultraguard-security-suite' ),
                                'button_style' => 'primary',
                                'icon'         => 'dashicons-saved',
                            ],
                        ],
                    ],
                ],
                $promo_grid,
                $promo_detail_card,
            ],
            'form'      => [
                'enable' => true,
                'id'     => 'upgrade-manager-activate-form',
                'ajax'   => true,
                'action' => 'ultraguard_upgrade_manager_activate_license',
                'nonce'  => $nonce,
            ],
        ];
    }
}
