<?php
/**
 * Product Barcode Widget - Render Methods
 *
 * @package GGM_Commerce
 */

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

/**
 * Render the premium widget content.
 *
 * @param GGM_Commerce_Widget_Product_Barcode $widget The widget instance.
 */
function ggmc_product_barcode_render_premium_content( $widget ) {
	$settings     = $widget->get_settings_for_display();
	$product_data = $widget->get_product_data();
	$is_editor    = ggmc_is_editor_mode();

	// Get product ID.
	$product_id = null;
	if ( $product_data && isset( $product_data->id ) ) {
		$product_id = $product_data->id;
	}

	// Get barcodes.
	$barcodes = array();
	if ( $product_id && class_exists( 'GGM_Commerce_Barcode' ) ) {
		$barcode_manager = GGM_Commerce_Barcode::get_instance();

		if ( 'all' === $settings['barcode_display'] ) {
			$barcodes = $barcode_manager->get_product_barcodes( $product_id );
		} else {
			$primary = $barcode_manager->get_primary_barcode( $product_id );
			if ( $primary ) {
				$barcodes = array( $primary );
			}
		}
	}

	// In editor, show placeholder if no barcodes.
	if ( $is_editor && empty( $barcodes ) ) {
		$barcodes = array(
			(object) array(
				'barcode_value' => '5901234123457',
				'barcode_type'  => 'EAN-13',
				'is_primary'    => 1,
			),
		);
	}

	// Hide if empty and setting enabled.
	if ( empty( $barcodes ) && 'yes' === $settings['hide_if_empty'] ) {
		return;
	}

	$layout = $settings['layout'];
	?>
	<div class="ggmc-product-barcode layout-<?php echo esc_attr( $layout ); ?>">
		<?php if ( empty( $barcodes ) ) : ?>
			<span class="ggmc-barcode-empty">No barcode</span>
		<?php else : ?>
			<?php foreach ( $barcodes as $barcode ) : ?>
				<div class="ggmc-barcode-item">
					<?php if ( ! empty( $settings['label_text'] ) ) : ?>
						<span class="ggmc-barcode-label"><?php echo esc_html( $settings['label_text'] ); ?></span>
					<?php endif; ?>

					<code class="ggmc-barcode-value"><?php echo esc_html( $barcode->barcode_value ); ?></code>

					<?php if ( 'yes' === $settings['show_barcode_type'] ) : ?>
						<span class="ggmc-barcode-type"><?php echo esc_html( $barcode->barcode_type ); ?></span>
					<?php endif; ?>

					<?php if ( $barcode->is_primary && 'all' === $settings['barcode_display'] ) : ?>
						<span class="ggmc-barcode-primary" title="Primary Barcode">&#9733;</span>
					<?php endif; ?>
				</div>
			<?php endforeach; ?>
		<?php endif; ?>
	</div>
	<?php // Styles moved to widgets.css (structural) and professional-defaults.css (design) ?>
	<?php
}
