<?php

declare(strict_types=1);

namespace Australcode\Rewards\Admin;

use Australcode\Rewards\Service\Licensing\LicenseGate;

defined( 'ABSPATH' ) || exit;

/**
 * "Upgrade" page — the unified growth surface (decision 8.117).
 *
 * Registered as the LAST submenu (after Freemius's native Account/Pricing
 * sub-pages), it merges in a single screen the Pro pitch + the native license
 * CTA + the family cross-sell ("More by Australcode"). It replaces the separate
 * "Family" submenu: one growth menu item per plugin — less invasive, lifts the
 * ticket. The kit partial `assets/acds/upgrade-page.php` embeds `family-page.php`
 * (bundled catalog, zero remote calls, market segmentation 8.112), so the family
 * surface lives inside Upgrade now.
 *
 * Capability degradation (UpgradeCta pattern): the native Freemius
 * pricing/account screens require `manage_options`; a shop manager
 * (`manage_woocommerce` without `manage_options`) who follows the link hits a
 * `wp_die`. So: admin → button; everyone else → "ask an administrator" note.
 * Free (it ships in the free ZIP — it is the cross-sell + upsell surface).
 */
final class UpgradePage {

	use RendersPageHeader;

	private const CAPABILITY = 'manage_woocommerce';
	private const PAGE_SLUG  = 'acrwd-upgrade';

	public function __construct( private LicenseGate $gate ) {}

	public function register(): void {
		// Upgrade is a free-build upsell surface: hide it once the store is on Pro —
		// an "Upgrade" menu makes no sense to an existing Pro customer (visual-pass
		// finding 2026-06-23). Priority 12: the slot vacated by the retired FamilyPage,
		// after the native Freemius Account/Pricing sub-pages — so "Upgrade" stays last.
		if ( $this->gate->is_pro_active() ) {
			return;
		}
		add_action( 'admin_menu', array( $this, 'add_submenu' ), 12 );
	}

	public function add_submenu(): void {
		add_submenu_page(
			AdminMenu::ROOT_SLUG,
			__( 'Upgrade — Australcode Rewards', 'australcode-points-rewards' ),
			__( 'Upgrade', 'australcode-points-rewards' ),
			self::CAPABILITY,
			self::PAGE_SLUG,
			array( $this, 'render' )
		);
	}

	public function render(): void {
		if ( ! current_user_can( self::CAPABILITY ) ) {
			wp_die( esc_html__( 'You are not allowed to access this page.', 'australcode-points-rewards' ) );
		}

		// Contract expected by assets/acds/upgrade-page.php (safe defaults inside).
		$acds_upgrade_url        = $this->gate->upgrade_url();
		$acds_can_manage_license = UpgradeCta::can_manage_license();
		$acds_is_pro             = $this->gate->is_pro_active();
		$acds_pro_features       = array(
			__( 'Earning rules engine with conditions, priority and a dry-run preview', 'australcode-points-rewards' ),
			__( 'Advanced redemption: points become single-use coupons (fixed / percent / free shipping)', 'australcode-points-rewards' ),
			__( 'VIP tiers with earning multipliers, badges and a progress bar', 'australcode-points-rewards' ),
			__( 'Time-boxed campaigns that stack on the base rate', 'australcode-points-rewards' ),
			__( 'Bonus points: signup, daily login, first order, review, birthday, profile, order-count, cart-total and lifetime spend', 'australcode-points-rewards' ),
			__( 'Two-sided referrals that reward both sides', 'australcode-points-rewards' ),
			__( 'Pro customer management: bulk actions, CSV import and block/unblock', 'australcode-points-rewards' ),
			__( 'Dedicated emails: expiry reminder and manual-adjustment notice', 'australcode-points-rewards' ),
			__( 'Points expiration to keep balances healthy', 'australcode-points-rewards' ),
		);
		// Variables the embedded family-page.php catalog partial expects.
		$acds_family_current = 'australcode-points-rewards';
		$acds_family_prefix  = 'acrwd';

		echo '<div class="wrap acds-root acrwd-page"><div class="acds-wrap">';
		$this->render_page_header(
			__( 'Upgrade', 'australcode-points-rewards' ),
			__( 'Unlock Pro growth features — and discover the rest of the Australcode family.', 'australcode-points-rewards' ),
			$this->dashboard_back_action()
		);
		require ACRWD_PATH . 'assets/acds/upgrade-page.php';
		echo '</div></div>';
	}
}
