<?php
/**
 * Informational "Pro Version" admin page.
 *
 * Surfaces under Listings → Pro Version. Points out which features are
 * available through the SEPARATE Pro plugin (feature cards + Free vs Pro
 * table) and links to themerex.net for everything else. Per wp.org Plugin
 * Directory Guideline 5 the page is strictly informational: no pricing, no
 * checkout, no third-party scripts — purchasing happens entirely off-site
 * (2026-07 review round 2).
 *
 * Hidden when the Pro plugin is already active.
 *
 * @package ThemeREX\AutoDealer\Admin
 */

declare(strict_types=1);

namespace ThemeREX\AutoDealer\Admin;

defined( 'ABSPATH' ) || exit;

class UpgradePage {

	public const PAGE_SLUG = 'trx-auto-dealer-upgrade';

	public function register(): void {
		add_action( 'admin_menu', array( $this, 'addPage' ), 999 );
		add_action( 'admin_enqueue_scripts', array( $this, 'enqueueAssets' ) );
		add_filter( 'plugin_action_links_' . TRX_AD_BASENAME, array( $this, 'addPluginActionLink' ) );
	}

	/**
	 * Enqueue the page-scoped CSS. No JS and no third-party scripts — the
	 * page is static.
	 */
	public function enqueueAssets( string $hook ): void {
		if ( $hook !== 'trx_ad_listing_page_' . self::PAGE_SLUG ) {
			return;
		}

		wp_enqueue_style( 'dashicons' );

		wp_enqueue_style(
			'trx-ad-upgrade-page',
			TRX_AD_URL . 'assets/css/upgrade-page.css',
			array(),
			TRX_AD_VERSION
		);
	}

	/**
	 * Append a "Pro version" link to the Plugins admin row.
	 *
	 * @param array<int|string, string> $links Existing action links.
	 * @return array<int|string, string>
	 */
	public function addPluginActionLink( $links ) {
		if ( $this->proIsActive() || ! is_array( $links ) ) {
			return $links;
		}

		$url = admin_url( 'edit.php?post_type=trx_ad_listing&page=' . self::PAGE_SLUG );

		$links[] = sprintf(
			'<a href="%s">%s</a>',
			esc_url( $url ),
			esc_html__( 'Pro version', 'trx-auto-dealer' )
		);

		return $links;
	}

	/**
	 * Hide the page entirely when the Pro plugin is already active.
	 */
	protected function proIsActive(): bool {
		return defined( 'TRX_AD_PRO_VERSION' );
	}

	public function addPage(): void {
		if ( $this->proIsActive() ) {
			return;
		}

		add_submenu_page(
			'edit.php?post_type=trx_ad_listing',
			__( 'Pro Version', 'trx-auto-dealer' ),
			__( 'Pro Version', 'trx-auto-dealer' ),
			'manage_options',
			self::PAGE_SLUG,
			array( $this, 'renderPage' ),
			999
		);
	}

	public function renderPage(): void {
		if ( ! current_user_can( 'manage_options' ) ) {
			wp_die( esc_html__( 'You do not have permission to access this page.', 'trx-auto-dealer' ), '', array( 'response' => 403 ) );
		}

		$landing_url = ProUpgrade::landingPageUrl();

		echo '<div class="wrap trx-ad-upgrade">';

		// ----------------------------------------------------------------
		// Hero
		// ----------------------------------------------------------------
		?>
		<div class="trx-ad-upgrade__hero">
			<h1><?php esc_html_e( 'Auto Dealer Pro', 'trx-auto-dealer' ); ?></h1>
			<p><?php esc_html_e( 'A separate Pro plugin, available from themerex.net, adds Lead CRM, multi-dealer marketplace, CSV/XML imports, marketplace feeds, test-drive bookings, geo search, and more on top of this free plugin.', 'trx-auto-dealer' ); ?></p>
			<p>
				<a href="<?php echo esc_url( $landing_url ); ?>" class="button button-primary button-hero" target="_blank" rel="noopener">
					<?php esc_html_e( 'Learn more on themerex.net', 'trx-auto-dealer' ); ?>
					<span aria-hidden="true"> &rarr;</span>
				</a>
			</p>
		</div>
		<?php

		// ----------------------------------------------------------------
		// Feature cards
		// ----------------------------------------------------------------
		?>
		<div class="trx-ad-upgrade__section">
			<h2><?php esc_html_e( 'What the Pro plugin adds', 'trx-auto-dealer' ); ?></h2>
			<div class="trx-ad-features">
				<?php foreach ( ProUpgrade::proFeatures() as $feature ) : ?>
					<div class="trx-ad-features__card">
						<div class="trx-ad-features__icon">
							<span class="dashicons dashicons-<?php echo esc_attr( $feature['icon'] ); ?>"></span>
						</div>
						<div class="trx-ad-features__body">
							<h3><?php echo esc_html( $feature['name'] ); ?></h3>
							<p><?php echo esc_html( $feature['summary'] ); ?></p>
						</div>
					</div>
				<?php endforeach; ?>
			</div>
		</div>
		<?php

		// ----------------------------------------------------------------
		// Free vs Pro comparison
		// ----------------------------------------------------------------
		?>
		<div class="trx-ad-upgrade__section">
			<h2><?php esc_html_e( 'Free vs Pro', 'trx-auto-dealer' ); ?></h2>
			<table class="trx-ad-compare">
				<thead>
					<tr>
						<th><?php esc_html_e( 'Capability', 'trx-auto-dealer' ); ?></th>
						<th><?php esc_html_e( 'Free', 'trx-auto-dealer' ); ?></th>
						<th><?php esc_html_e( 'Pro', 'trx-auto-dealer' ); ?></th>
					</tr>
				</thead>
				<tbody>
					<?php
					foreach ( ProUpgrade::comparisonRows() as $row ) :
						$free_class = ( '—' === $row['free'] ) ? 'trx-ad-compare__no' : 'trx-ad-compare__yes';
						$pro_class  = ( '—' === $row['pro'] ) ? 'trx-ad-compare__no' : 'trx-ad-compare__yes';
						?>
						<tr>
							<td><?php echo esc_html( $row['capability'] ); ?></td>
							<td class="<?php echo esc_attr( $free_class ); ?>"><?php echo esc_html( $row['free'] ); ?></td>
							<td class="<?php echo esc_attr( $pro_class ); ?>"><?php echo esc_html( $row['pro'] ); ?></td>
						</tr>
					<?php endforeach; ?>
				</tbody>
			</table>
		</div>
		<?php

		// ----------------------------------------------------------------
		// Practical notes
		// ----------------------------------------------------------------
		?>
		<div class="trx-ad-upgrade__section">
			<div class="trx-ad-faq">
				<div class="trx-ad-faq__item">
					<h4><?php esc_html_e( 'What happens to my data if I install Pro?', 'trx-auto-dealer' ); ?></h4>
					<p><?php esc_html_e( 'Pro reads the same custom post types, taxonomies, and DB tables as this plugin. All listings, dealers, settings, and templates carry over automatically. Pro adds new features on top, never replaces what you have.', 'trx-auto-dealer' ); ?></p>
				</div>
				<div class="trx-ad-faq__item">
					<h4><?php esc_html_e( 'Is the Pro plugin GPL?', 'trx-auto-dealer' ); ?></h4>
					<p><?php esc_html_e( 'Yes. Pro is GPL-2.0-or-later, same as this plugin, and is distributed separately from themerex.net.', 'trx-auto-dealer' ); ?></p>
				</div>
			</div>
		</div>
		<?php

		echo '</div>'; // .wrap
	}
}
