<?php
/**
 * Stripe Gateway Widget - Render Functions
 *
 * @package GGM_Commerce
 */

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

/**
 * Render the premium widget content.
 *
 * @param GGM_Commerce_Widget_Stripe_Gateway $widget Widget instance.
 * @return void
 */
function ggmc_stripe_gateway_render_premium_content( $widget ) {
	$settings = $widget->get_settings_for_display();
	$gateway  = ggm_commerce_gateways()->get_gateway( 'stripe' );

	// Check if gateway is configured.
	if ( ! $gateway || ! $gateway->is_configured() ) {
		if ( ggmc_is_editor_mode() ) {
			?>
			<div class="ggmc-stripe-container ggmc-not-configured" style="padding: 20px; background: #fff3cd; border: 1px solid #ffc107; border-radius: 4px;">
				<p style="margin: 0; color: #856404;">
					<strong><?php esc_html_e( 'Stripe Not Configured', 'ggm-commerce' ); ?></strong><br>
					<?php esc_html_e( 'Please configure your Stripe API keys in GGM Commerce → Settings → Payment.', 'ggm-commerce' ); ?>
				</p>
			</div>
			<?php
		}
		return;
	}

	?>
	<div class="ggmc-stripe-container">
		<?php if ( 'yes' === $settings['show_title'] ) : ?>
			<h4 class="ggmc-stripe-title"><?php echo esc_html( $settings['title_text'] ); ?></h4>
		<?php endif; ?>

		<?php if ( 'yes' === $settings['show_icons'] ) : ?>
			<div class="ggmc-payment-icons">
				<span class="ggmc-card-icon ggmc-visa" title="Visa"></span>
				<span class="ggmc-card-icon ggmc-mastercard" title="Mastercard"></span>
				<span class="ggmc-card-icon ggmc-amex" title="American Express"></span>
				<?php if ( 'yes' === $gateway->get_option( 'enable_apple_pay', 'yes' ) ) : ?>
					<span class="ggmc-card-icon ggmc-apple-pay" title="Apple Pay"></span>
				<?php endif; ?>
				<?php if ( 'yes' === $gateway->get_option( 'enable_google_pay', 'yes' ) ) : ?>
					<span class="ggmc-card-icon ggmc-google-pay" title="Google Pay"></span>
				<?php endif; ?>
			</div>
		<?php endif; ?>

		<div id="ggmc-stripe-payment-element">
			<!-- Stripe Payment Element will be mounted here by JavaScript -->
		</div>
		<div id="ggmc-stripe-payment-message" class="ggmc-payment-message" style="display: none;"></div>
	</div>
	<?php

	// Enqueue Stripe scripts.
	ggmc_stripe_gateway_enqueue_stripe_scripts( $gateway );
}

/**
 * Enqueue Stripe JavaScript.
 *
 * @param GGM_Commerce_Gateway_Stripe $gateway Gateway instance.
 * @return void
 */
function ggmc_stripe_gateway_enqueue_stripe_scripts( $gateway ) {
	// Only enqueue on frontend, not in editor.
	if ( ggmc_is_editor_mode() ) {
		return;
	}

	wp_enqueue_script( 'stripe-js', 'https://js.stripe.com/v3/', array(), null, true );

	wp_enqueue_script(
		'ggmc-stripe-payment',
		plugins_url( 'assets/js/stripe-payment.js', dirname( __DIR__ ) . '/goose-commerce.php' ),
		array( 'jquery', 'stripe-js' ),
		'1.1.1-' . time(),
		true
	);

	wp_localize_script( 'ggmc-stripe-payment', 'ggmcStripe', $gateway->get_js_data() );
}
