<?php
/**
 * Recently Viewed Widget - Render Methods
 *
 * @package GGM_Commerce
 */

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

// phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery
// phpcs:disable WordPress.DB.DirectDatabaseQuery.NoCaching
// phpcs:disable PluginCheck.Security.DirectDB.UnescapedDBParameter

/**
 * Render the premium widget content.
 *
 * @param \GGM_Commerce_Widget_Recently_Viewed $widget The widget instance.
 */
function ggmc_recently_viewed_render_premium_content( $widget ) {
	$settings        = $widget->get_settings_for_display();
	$max_products    = absint( $settings['max_products'] );
	$exclude_current = 'yes' === $settings['exclude_current'];
	$hover_effect    = $settings['image_hover_effect'] ?? 'zoom';

	// Get recently viewed products.
	$products = array();
	if ( class_exists( 'GGM_Commerce_Recently_Viewed' ) ) {
		$products = GGM_Commerce_Recently_Viewed::get_viewed_products( $max_products, $exclude_current );
	}

	// For editor preview, show sample products if no viewed products.
	if ( empty( $products ) && ggmc_is_editor_mode() ) {
		global $wpdb;
		$table_name = $wpdb->prefix . 'ggmc_products';
		// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- $table_name is safely constructed
		$products = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM $table_name WHERE status = 'active' ORDER BY id DESC LIMIT %d", $max_products ) );
	}

	$hover_class = 'none' !== $hover_effect ? 'ggmc-hover-' . $hover_effect : '';
	?>

	<div class="ggmc-recently-viewed-widget">
		<?php if ( 'yes' === $settings['show_title'] && ! empty( $settings['title'] ) ) : ?>
			<h3 class="ggmc-recently-viewed-title"><?php echo esc_html( $settings['title'] ); ?></h3>
		<?php endif; ?>

		<?php if ( empty( $products ) ) : ?>
			<p class="ggmc-recently-viewed-empty"><?php echo esc_html( $settings['empty_message'] ); ?></p>
		<?php else : ?>
			<div class="ggmc-recently-viewed-grid">
				<?php foreach ( $products as $product ) : ?>
					<?php
					$product_url = GGM_Commerce_Helpers::get_product_details_url( $product->id );
					$price_html  = GGM_Commerce_Helpers::format_price( $product->price );
					// Convert image_id to URL.
					$image_url = '';
					if ( ! empty( $product->image_id ) ) {
						$image_url = wp_get_attachment_image_url( $product->image_id, 'medium' );
					}
					?>
					<div class="ggmc-recently-viewed-item <?php echo esc_attr( $hover_class ); ?>">
						<?php if ( 'yes' === $settings['show_image'] ) : ?>
							<a href="<?php echo esc_url( $product_url ); ?>" class="ggmc-recently-viewed-item-image">
								<?php if ( ! empty( $image_url ) ) : ?>
									<img src="<?php echo esc_url( $image_url ); ?>"
										 alt="<?php echo esc_attr( $product->name ); ?>">
								<?php else : ?>
									<div class="ggmc-recently-viewed-placeholder">
										<span class="dashicons dashicons-format-image"></span>
									</div>
								<?php endif; ?>
							</a>
						<?php endif; ?>
						<div class="ggmc-recently-viewed-item-content">
							<h4 class="ggmc-recently-viewed-item-name">
								<a href="<?php echo esc_url( $product_url ); ?>">
									<?php echo esc_html( $product->name ); ?>
								</a>
							</h4>
							<?php if ( 'yes' === $settings['show_price'] ) : ?>
								<span class="ggmc-recently-viewed-item-price">
									<?php echo wp_kses_post( $price_html ); ?>
								</span>
							<?php endif; ?>
							<?php if ( 'yes' === $settings['show_add_to_cart'] ) : ?>
								<button type="button"
									class="ggmc-recently-viewed-add-to-cart ggmc-add-to-cart-btn"
									data-product-id="<?php echo esc_attr( $product->id ); ?>">
									<?php echo esc_html( $settings['add_to_cart_text'] ); ?>
								</button>
							<?php endif; ?>
						</div>
					</div>
				<?php endforeach; ?>
			</div>
		<?php endif; ?>
	</div>

	<?php
}
