<?php
namespace Pay4AllForMypos\Backend;

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

/**
 * « Add-ons » admin sub-page listing the other Pay4All plugins.
 * Same layout as pay4all-win : one card per companion plugin with an
 * « active » banner when the plugin is loaded, otherwise a search-link
 * that opens the WP.org installer + a « learn more » link.
 */
final class AddOnsPage {

	public const SLUG = 'pay4all-for-mypos-addons';

	public static function render() : void {
		if ( ! current_user_can( 'manage_options' ) ) {
			wp_die( esc_html__( 'You do not have permission.', 'pay4all-for-mypos' ) );
		}
		if ( ! function_exists( 'is_plugin_active' ) ) {
			require_once ABSPATH . 'wp-admin/includes/plugin.php';
		}

		$lang = self::current_lang();

		$cards = [
			[
				'title'  => __( 'Pay4All Shop', 'pay4all-for-mypos' ),
				'desc'   => __( 'Order forms with quantity rules, delivery methods, customer directory and multilingual invoices.', 'pay4all-for-mypos' ),
				'active' => is_plugin_active( 'pay4all-form/pay4all-form.php' ) || class_exists( '\\Pay4All\\Form\\Domain\\Customer' ),
				'search' => 'pay4all+shop',
				'more'   => add_query_arg( 'lang', $lang, 'https://pay4all.net/p/pay4all-shop' ),
			],
			[
				'title'  => __( 'Pay4All for TWINT', 'pay4all-for-mypos' ),
				'desc'   => __( 'Automated TWINT payments on your forms and WooCommerce checkout.', 'pay4all-for-mypos' ),
				'active' => defined( 'PAY4ALL_FOR_TWINT_PLUGIN_VERSION' ) || class_exists( '\\Twnt4All\\Bootstrap' ),
				'search' => 'pay4all+for+twint',
				'more'   => add_query_arg( 'lang', $lang, 'https://pay4all.net/p/twint-woocommerce' ),
			],
			[
				'title'  => __( 'Pay4All Stripe', 'pay4all-for-mypos' ),
				'desc'   => __( 'Card payment via Stripe Checkout (Visa, Mastercard, Apple Pay, Google Pay, Link).', 'pay4all-for-mypos' ),
				'active' => defined( 'PAY4ALL_FOR_STRIPE_PLUGIN_VERSION' ) || class_exists( '\\Pay4AllForStripe\\Backend\\ConfigPage' ),
				'search' => 'pay4all+stripe',
				'more'   => add_query_arg( 'lang', $lang, 'https://pay4all.net/p/stripe-form-order' ),
			],
			[
				'title'  => __( 'Pay4All Age Verification', 'pay4all-for-mypos' ),
				'desc'   => __( 'Age verification via ID document and selfie with a cross-device QR flow.', 'pay4all-for-mypos' ),
				'active' => is_plugin_active( 'pay4all-age-verification/pay4all-age-verification.php' ),
				'search' => 'pay4all+age+verification',
				'more'   => add_query_arg( 'lang', $lang, 'https://pay4all.net/p/shop-age-verification' ),
			],
			[
				'title'  => __( 'Pay4All WIN', 'pay4all-for-mypos' ),
				'desc'   => __( 'Multilingual online contests with QR codes and random winner draw.', 'pay4all-for-mypos' ),
				'active' => defined( 'P4ALL_WIN_VERSION' ),
				'search' => 'pay4all+win',
				'more'   => add_query_arg( 'lang', $lang, 'https://pay4all.net/p/pay4all-win' ),
			],
		];

		?>
		<div class="wrap">
			<h1><?php esc_html_e( 'Pay4All add-ons', 'pay4all-for-mypos' ); ?></h1>
			<p style="max-width:780px;color:#555;">
				<?php esc_html_e( 'Each add-on is independent: install only the ones you need.', 'pay4all-for-mypos' ); ?>
			</p>
			<div style="display:grid;grid-template-columns:repeat(auto-fit,minmax(320px,1fr));gap:18px;max-width:1200px;">
				<?php foreach ( $cards as $card ) :
					$install_url = self_admin_url(
						'plugin-install.php?s=' . rawurlencode( (string) $card['search'] ) . '&tab=search&type=term'
					);
				?>
					<section style="background:#fff;border:1px solid #dcdcde;border-radius:6px;padding:16px 18px;">
						<h2 style="margin-top:0;"><?php echo esc_html( (string) $card['title'] ); ?></h2>
						<p style="color:#555;"><?php echo esc_html( (string) $card['desc'] ); ?></p>
						<?php if ( ! empty( $card['active'] ) ) : ?>
							<p style="margin:14px 0 0;color:#007a2f;">
								<strong>&#10003; <?php esc_html_e( 'Active on this site.', 'pay4all-for-mypos' ); ?></strong>
							</p>
						<?php else : ?>
							<p style="margin:14px 0 0;">
								<a class="button button-primary" href="<?php echo esc_url( $install_url ); ?>">
									<?php esc_html_e( 'Install from WordPress', 'pay4all-for-mypos' ); ?>
								</a>
								<a class="button" href="<?php echo esc_url( (string) $card['more'] ); ?>"
								   target="_blank" rel="noopener noreferrer" style="margin-left:6px;">
									<?php esc_html_e( 'Learn more', 'pay4all-for-mypos' ); ?> &#8599;
								</a>
							</p>
						<?php endif; ?>
					</section>
				<?php endforeach; ?>
			</div>
		</div>
		<?php
	}

	public static function current_lang() : string {
		$lang = strtolower( substr( (string) determine_locale(), 0, 2 ) );
		return in_array( $lang, [ 'fr', 'de', 'it', 'en' ], true ) ? $lang : 'fr';
	}
}
