<?php
/**
 * Upgrade to Pro page.
 *
 * Renders the in-admin pricing page that promotes Donatico Pro. Visible
 * only when the Pro add-on plugin is inactive. See docs/WEEK6-UPSELL-SPEC.md
 * for the full specification.
 *
 * @package Donatico\Admin
 */

declare(strict_types=1);

namespace Donatico\Admin;

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

/**
 * Renders the Upgrade to Pro page in the Donatico admin menu.
 */
class UpgradePage {

	/**
	 * Render the upgrade page.
	 *
	 * @return void
	 */
	public function render(): void {
		if ( ! current_user_can( 'manage_options' ) ) {
			return;
		}

		$banner_url = DONATICO_PLUGIN_URL . 'assets/upgrade/banner.png';
		$plans      = $this->get_plans();
		$features   = $this->get_feature_comparison();
		?>
		<div class="wrap donatico-upgrade-wrap">

			<div class="donatico-upgrade-hero">
				<img
					src="<?php echo esc_url( $banner_url ); ?>"
					alt="<?php esc_attr_e( 'Donatico Pro', 'donatico' ); ?>"
					class="donatico-upgrade-banner"
				/>
			</div>

			<div class="donatico-upgrade-intro">
				<h1><?php esc_html_e( 'Upgrade to Donatico Pro', 'donatico' ); ?></h1>
				<p class="donatico-upgrade-tagline">
					<?php esc_html_e( 'Recurring donations, donor management, campaigns, and more — built on the Donatico foundation you already trust.', 'donatico' ); ?>
				</p>
			</div>

			<div class="donatico-upgrade-pricing">
				<?php foreach ( $plans as $plan ) : ?>
					<div class="donatico-plan-card <?php echo $plan['featured'] ? 'is-featured' : ''; ?>">
						<?php if ( $plan['featured'] ) : ?>
							<div class="donatico-plan-badge"><?php esc_html_e( 'Most Popular', 'donatico' ); ?></div>
						<?php endif; ?>

						<h2 class="donatico-plan-name"><?php echo esc_html( $plan['name'] ); ?></h2>
						<p class="donatico-plan-sites"><?php echo esc_html( $plan['sites'] ); ?></p>

						<div class="donatico-plan-price">
							<span class="donatico-price-currency">$</span>
							<span class="donatico-price-amount"><?php echo esc_html( (string) $plan['annual_price'] ); ?></span>
							<span class="donatico-price-period">/ <?php esc_html_e( 'year', 'donatico' ); ?></span>
						</div>

						<p class="donatico-plan-lifetime">
							<?php
							printf(
								/* translators: %s: lifetime price formatted as currency */
								esc_html__( 'or $%s lifetime', 'donatico' ),
								esc_html( (string) $plan['lifetime_price'] )
							);
							?>
						</p>

						<ul class="donatico-plan-features">
							<?php foreach ( $plan['features'] as $feature ) : ?>
								<li>
									<span class="donatico-check" aria-hidden="true">✓</span>
									<?php echo esc_html( $feature ); ?>
								</li>
							<?php endforeach; ?>
						</ul>

						<button
							type="button"
							class="button button-primary button-hero donatico-cta-button"
							data-fs-plugin-id="30154"
							data-fs-plan-id="<?php echo esc_attr( (string) $plan['fs_plan_id'] ); ?>"
							data-fs-licenses="<?php echo esc_attr( (string) $plan['fs_licenses'] ); ?>"
							data-fs-plan-name="<?php echo esc_attr( $plan['name'] ); ?>"
						>
							<?php
							printf(
								/* translators: %s: plan name (Starter, Business, or Agency) */
								esc_html__( 'Get Pro %s', 'donatico' ),
								esc_html( $plan['name'] )
							);
							?>
						</button>
					</div>
				<?php endforeach; ?>
			</div>

			<div class="donatico-upgrade-guarantee">
				<span class="donatico-guarantee-icon" aria-hidden="true">★</span>
				<div>
					<strong><?php esc_html_e( '14-Day Money-Back Guarantee', 'donatico' ); ?></strong>
					<p><?php esc_html_e( 'Try Donatico Pro risk-free. If it is not the right fit within 14 days, we refund 100% — no questions asked.', 'donatico' ); ?></p>
				</div>
			</div>

			<div class="donatico-upgrade-comparison">
				<h2><?php esc_html_e( 'What you get with Pro', 'donatico' ); ?></h2>
				<table class="donatico-comparison-table widefat striped">
					<thead>
						<tr>
							<th class="donatico-feature-name"><?php esc_html_e( 'Feature', 'donatico' ); ?></th>
							<th class="donatico-tier-col"><?php esc_html_e( 'Free', 'donatico' ); ?></th>
							<th class="donatico-tier-col donatico-tier-pro"><?php esc_html_e( 'Pro', 'donatico' ); ?></th>
						</tr>
					</thead>
					<tbody>
						<?php foreach ( $features as $row ) : ?>
							<tr>
								<td class="donatico-feature-name"><?php echo esc_html( $row['feature'] ); ?></td>
								<td><?php echo wp_kses_post( $this->render_availability( $row['free'] ) ); ?></td>
								<td class="donatico-tier-pro"><?php echo wp_kses_post( $this->render_availability( $row['pro'] ) ); ?></td>
							</tr>
						<?php endforeach; ?>
					</tbody>
				</table>
			</div>

			<div class="donatico-upgrade-footer">
				<p>
					<?php
					printf(
						/* translators: %s: support email address */
						esc_html__( 'Questions? Contact %s — happy to help with setup or any questions before you upgrade.', 'donatico' ),
						'<a href="mailto:mamun.rasid@gmail.com">mamun.rasid@gmail.com</a>'
					);
					?>
				</p>
			</div>

		</div>
		<?php
	}

	/**
	 * Pricing plan data.
	 *
	 * Keep these in sync with the Freemius dashboard plan IDs and prices.
	 * Plan IDs and prices are static — Freemius config is the source of truth
	 * but this array is what the page actually renders.
	 *
	 * @return array<int, array<string, mixed>>
	 */
	private function get_plans(): array {
		return array(
			array(
				'name'           => 'Starter',
				'fs_plan_id'     => 52115,
				'fs_licenses'    => 1,
				'sites'          => __( '1 site', 'donatico' ),
				'annual_price'   => 49,
				'lifetime_price' => 99,
				'featured'       => false,
				'features'       => array(
					__( 'Recurring donations (monthly, yearly)', 'donatico' ),
					__( 'Donation tiers with custom amounts', 'donatico' ),
					__( 'Tribute donations (memorial / in honor)', 'donatico' ),
					__( 'Donor management dashboard', 'donatico' ),
					__( 'Donor wall shortcode', 'donatico' ),
					__( 'Campaigns with goals', 'donatico' ),
					__( 'Stripe Customer Portal', 'donatico' ),
					__( '1 year of updates and support', 'donatico' ),
				),
			),
			array(
				'name'           => 'Business',
				'fs_plan_id'     => 52117,
				'fs_licenses'    => 5,
				'sites'          => __( 'Up to 5 sites', 'donatico' ),
				'annual_price'   => 99,
				'lifetime_price' => 299,
				'featured'       => true,
				'features'       => array(
					__( 'Everything in Starter', 'donatico' ),
					__( 'Use on up to 5 websites', 'donatico' ),
					__( 'Priority email support', 'donatico' ),
					__( '1 year of updates and support', 'donatico' ),
				),
			),
			array(
				'name'           => 'Agency',
				'fs_plan_id'     => 52120,
				'fs_licenses'    => 0,
				'sites'          => __( 'Unlimited sites', 'donatico' ),
				'annual_price'   => 199,
				'lifetime_price' => 599,
				'featured'       => false,
				'features'       => array(
					__( 'Everything in Business', 'donatico' ),
					__( 'Unlimited site activations', 'donatico' ),
					__( 'White-label ready', 'donatico' ),
					__( '1 year of updates and support', 'donatico' ),
				),
			),
		);
	}

	/**
	 * Feature comparison data for the Free vs Pro table.
	 *
	 * Each row: feature name + availability in Free and Pro. Availability
	 * can be true (checkmark), false (dash), or a string label (custom text).
	 *
	 * @return array<int, array<string, mixed>>
	 */
	private function get_feature_comparison(): array {
		return array(
			array(
				'feature' => __( 'One-time donations via Stripe', 'donatico' ),
				'free'    => true,
				'pro'     => true,
			),
			array(
				'feature' => __( 'Recurring donations (monthly, yearly)', 'donatico' ),
				'free'    => false,
				'pro'     => true,
			),
			array(
				'feature' => __( 'Donation tiers with custom amounts', 'donatico' ),
				'free'    => false,
				'pro'     => true,
			),
			array(
				'feature' => __( 'Campaigns with goals and progress tracking', 'donatico' ),
				'free'    => false,
				'pro'     => true,
			),
			array(
				'feature' => __( 'Tribute donations (memorial / in honor)', 'donatico' ),
				'free'    => false,
				'pro'     => true,
			),
			array(
				'feature' => __( 'Donor management dashboard', 'donatico' ),
				'free'    => false,
				'pro'     => true,
			),
			array(
				'feature' => __( 'Public donor wall shortcode', 'donatico' ),
				'free'    => false,
				'pro'     => true,
			),
			array(
				'feature' => __( 'Stripe Customer Portal for subscribers', 'donatico' ),
				'free'    => false,
				'pro'     => true,
			),
			array(
				'feature' => __( 'Donation form designer', 'donatico' ),
				'free'    => true,
				'pro'     => true,
			),
			array(
				'feature' => __( 'Email notifications to donors and admins', 'donatico' ),
				'free'    => true,
				'pro'     => true,
			),
			array(
				'feature' => __( 'Priority support', 'donatico' ),
				'free'    => false,
				'pro'     => true,
			),
		);
	}

	/**
	 * Render an availability indicator for a feature row.
	 *
	 * @param mixed $availability true (checkmark), false (dash), or string (custom).
	 * @return string Safe HTML.
	 */
	private function render_availability( $availability ): string {
		if ( true === $availability ) {
			return '<span class="donatico-yes" aria-label="' . esc_attr__( 'Included', 'donatico' ) . '">✓</span>';
		}
		if ( false === $availability ) {
			return '<span class="donatico-no" aria-label="' . esc_attr__( 'Not included', 'donatico' ) . '">—</span>';
		}
		return '<span class="donatico-custom">' . esc_html( (string) $availability ) . '</span>';
	}
}
