<?php
/**
 * Admin screen: Square subscription plan variations + integration docs.
 *
 * @package SHC4WC
 */

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

class SHC4WC_Subscription_Settings {
	private static $instance = null;

	public static function instance() {
		if ( null === self::$instance ) {
			self::$instance = new self();
		}
		return self::$instance;
	}

	private function __construct() {
		add_action( 'admin_menu', array( $this, 'register_menu' ), 11 );
	}

	public function register_menu() {
		add_submenu_page(
			SHC4WC_Admin_Menu::MENU_SLUG,
			__( 'Subscription Plans', 'payments-connect-square' ),
			__( 'Subscription Plans', 'payments-connect-square' ),
			'manage_options',
			SHC4WC_Admin_Menu::SUBSCRIPTIONS_SLUG,
			array( $this, 'render_page' )
		);
	}

	public function render_page() {
		if ( ! current_user_can( 'manage_options' ) ) {
			return;
		}

		$refresh   = isset( $_GET['refresh'] ) && '1' === (string) $_GET['refresh']; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
		$connected = SHC4WC_Plugin::is_connected();
		$variations = $connected ? SHC4WC_Subscriptions::get_plan_variations( $refresh ) : array();
		$refresh_url = wp_nonce_url(
			SHC4WC_Admin_Menu::page_url( SHC4WC_Admin_Menu::SUBSCRIPTIONS_SLUG, array( 'refresh' => '1' ) ),
			'shc4wc_refresh_subscriptions'
		);

		if ( $refresh ) {
			check_admin_referer( 'shc4wc_refresh_subscriptions' );
		}
		?>
		<div class="wrap">
			<h1><?php esc_html_e( 'Subscription Plans', 'payments-connect-square' ); ?></h1>
			<p>
				<?php
				printf(
					/* translators: %s: Square documentation URL. */
					esc_html__( 'Square subscription plan variations from your catalog. Use them with Contact Form 7, Paid Memberships Pro, or WooCommerce Subscriptions via Square hosted checkout. %s', 'payments-connect-square' ),
					'<a href="https://developer.squareup.com/docs/checkout-api/subscription-plan-checkout" target="_blank" rel="noopener noreferrer">' . esc_html__( 'Subscription Plan Checkout docs', 'payments-connect-square' ) . '</a>'
				);
				?>
			</p>

			<?php if ( ! $connected ) : ?>
				<div class="notice notice-warning"><p>
					<?php
					printf(
						/* translators: %s: connect settings URL. */
						esc_html__( 'Set up Payments Square Connect first on the %s screen.', 'payments-connect-square' ),
						'<a href="' . esc_url( SHC4WC_Admin_Menu::page_url() ) . '">' . esc_html__( 'Payments Square Connect', 'payments-connect-square' ) . '</a>'
					);
					?>
				</p></div>
			<?php else : ?>
				<p>
					<a class="button" href="<?php echo esc_url( $refresh_url ); ?>"><?php esc_html_e( 'Refresh from Square', 'payments-connect-square' ); ?></a>
				</p>
			<?php endif; ?>

			<h2><?php esc_html_e( 'Available plan variations', 'payments-connect-square' ); ?></h2>
			<?php if ( $connected && empty( $variations ) ) : ?>
				<p><?php esc_html_e( 'No subscription plan variations found in Square Catalog. Create a plan and variation in Square first.', 'payments-connect-square' ); ?></p>
			<?php elseif ( ! empty( $variations ) ) : ?>
				<table class="widefat striped">
					<thead>
						<tr>
							<th><?php esc_html_e( 'Variation', 'payments-connect-square' ); ?></th>
							<th><?php esc_html_e( 'Plan', 'payments-connect-square' ); ?></th>
							<th><?php esc_html_e( 'Price', 'payments-connect-square' ); ?></th>
							<th><?php esc_html_e( 'Cadence', 'payments-connect-square' ); ?></th>
							<th><?php esc_html_e( 'Variation ID', 'payments-connect-square' ); ?></th>
						</tr>
					</thead>
					<tbody>
						<?php foreach ( $variations as $variation ) : ?>
							<tr>
								<td><?php echo esc_html( $variation['name'] ); ?></td>
								<td><?php echo esc_html( $variation['plan_name'] ?: '—' ); ?></td>
								<td><?php echo esc_html( number_format_i18n( $variation['amount'], 2 ) . ' ' . $variation['currency'] ); ?></td>
								<td><?php echo esc_html( $variation['cadence'] ?: '—' ); ?></td>
								<td><code><?php echo esc_html( $variation['id'] ); ?></code></td>
							</tr>
						<?php endforeach; ?>
					</tbody>
				</table>
			<?php endif; ?>

			<h2><?php esc_html_e( 'Contact Form 7', 'payments-connect-square' ); ?></h2>
			<p><?php esc_html_e( 'Add this tag to a form to start Square subscription checkout:', 'payments-connect-square' ); ?></p>
			<pre><code>[square_subscribe variation:YOUR_VARIATION_ID name:"Silver Membership" amount:15.00]</code></pre>
			<ul style="list-style:disc;margin-left:1.5em;">
				<li><code>variation</code> — <?php esc_html_e( 'Required. Square subscription plan variation ID (not the parent plan ID).', 'payments-connect-square' ); ?></li>
				<li><code>amount</code> — <?php esc_html_e( 'Optional. Initial phase price; should match the variation price in Square.', 'payments-connect-square' ); ?></li>
				<li><code>name</code> — <?php esc_html_e( 'Optional. Label shown on the Square checkout page.', 'payments-connect-square' ); ?></li>
			</ul>

			<h2><?php esc_html_e( 'WPForms', 'payments-connect-square' ); ?></h2>
			<p><?php esc_html_e( 'Add a Single Item payment field and a Square Hosted Checkout field, then enable Payments Square Connect under the form Payments panel. For subscriptions, paste a plan variation ID under Subscription signup.', 'payments-connect-square' ); ?></p>

			<h2><?php esc_html_e( 'Paid Memberships Pro', 'payments-connect-square' ); ?></h2>
			<?php if ( defined( 'PMPRO_VERSION' ) || class_exists( 'PMProGateway' ) ) : ?>
				<p>
					<?php
					printf(
						/* translators: 1: PMPro payment settings URL, 2: docs URL. */
						esc_html__( 'Under %1$s, choose Square Hosted Checkout (Payments Square Connect). Recurring membership levels redirect to Square; no WooCommerce Subscriptions required. %2$s', 'payments-connect-square' ),
						'<a href="' . esc_url( admin_url( 'admin.php?page=pmpro-paymentsettings' ) ) . '">' . esc_html__( 'PMPro → Settings → Payments', 'payments-connect-square' ) . '</a>',
						'<a href="https://payments-connect-square.pages.dev/paid-memberships-pro/?utm_source=wp_admin&utm_medium=subscription_plans&utm_campaign=pmpro" target="_blank" rel="noopener noreferrer">' . esc_html__( 'Paid Memberships Pro setup guide', 'payments-connect-square' ) . '</a>'
					);
					?>
				</p>
				<p><?php esc_html_e( 'Optional: edit a membership level and select a Square plan variation, or leave auto-match to create a plan from the level billing amount and cycle.', 'payments-connect-square' ); ?></p>
			<?php else : ?>
				<p>
					<?php
					printf(
						/* translators: %s: PMPro plugin URL. */
						esc_html__( 'Install %s to accept membership subscriptions with Square Hosted Checkout — no WooCommerce required.', 'payments-connect-square' ),
						'<a href="https://wordpress.org/plugins/paid-memberships-pro/" target="_blank" rel="noopener noreferrer">Paid Memberships Pro</a>'
					);
					?>
				</p>
			<?php endif; ?>

			<h2><?php esc_html_e( 'WooCommerce Subscriptions', 'payments-connect-square' ); ?></h2>
			<?php if ( class_exists( 'WC_Subscriptions' ) ) : ?>
				<p><?php esc_html_e( 'Edit a subscription product and choose a Square subscription plan variation under General. When the customer checks out with Payments Square Connect, they are sent to Square to subscribe instead of a one-time payment link.', 'payments-connect-square' ); ?></p>
			<?php else : ?>
				<p><?php esc_html_e( 'Install and activate WooCommerce Subscriptions to map subscription products to Square plan variations.', 'payments-connect-square' ); ?></p>
			<?php endif; ?>

			<h2><?php esc_html_e( 'Limitations', 'payments-connect-square' ); ?></h2>
			<ul style="list-style:disc;margin-left:1.5em;">
				<li><?php esc_html_e( 'Only plan variations with one paid phase (or one free + one paid phase) are supported by Square Checkout.', 'payments-connect-square' ); ?></li>
				<li><?php esc_html_e( 'Cash App Pay and Afterpay are not supported for subscription checkout.', 'payments-connect-square' ); ?></li>
			</ul>
		</div>
		<?php
	}
}
