<?php
/**
 * Custom Attributes Widget - Render Methods
 *
 * @package GGM_Commerce
 */

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

/**
 * Render the premium widget content.
 *
 * @param GGM_Commerce_Widget_Custom_Attributes $widget The widget instance.
 */
function ggmc_custom_attributes_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 attributes.
	$attributes = array();
	if ( $product_id && class_exists( 'GGM_Commerce_Attributes' ) ) {
		$attributes_manager = GGM_Commerce_Attributes::get_instance();
		$attributes         = $attributes_manager->get_product_attributes( $product_id );

		// Filter out empty if needed.
		if ( 'yes' !== $settings['show_empty'] ) {
			$attributes = array_filter(
				$attributes,
				function ( $attr ) {
					return ! empty( $attr->attribute_value );
				}
			);
		}

		// Filter to selected attributes only (if specified).
		$selected_ids = ! empty( $settings['selected_attributes'] ) ? array_map( 'intval', (array) $settings['selected_attributes'] ) : array();
		if ( ! empty( $selected_ids ) ) {
			$attributes = array_filter(
				$attributes,
				function ( $attr ) use ( $selected_ids ) {
					return in_array( (int) $attr->attribute_id, $selected_ids, true );
				}
			);
		}
	}

	// In editor, show placeholder if no attributes.
	if ( $is_editor && empty( $attributes ) ) {
		$attributes = array(
			(object) array(
				'name'            => 'Weight',
				'attribute_value' => '2.5',
				'unit'            => 'kg',
				'group_name'      => 'Dimensions',
				'icon'            => '{"value":"fas fa-weight-hanging","library":"fa-solid"}',
			),
			(object) array(
				'name'            => 'Height',
				'attribute_value' => '30',
				'unit'            => 'cm',
				'group_name'      => 'Dimensions',
				'icon'            => '{"value":"fas fa-ruler-vertical","library":"fa-solid"}',
			),
			(object) array(
				'name'            => 'Material',
				'attribute_value' => 'Stainless Steel',
				'unit'            => '',
				'group_name'      => 'Materials',
				'icon'            => '{"value":"fas fa-layer-group","library":"fa-solid"}',
			),
			(object) array(
				'name'            => 'Color',
				'attribute_value' => 'Silver',
				'unit'            => '',
				'group_name'      => 'Materials',
				'icon'            => '{"value":"fas fa-palette","library":"fa-solid"}',
			),
		);
	}

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

	$layout  = $settings['layout'];
	$columns = $settings['columns'];

	// Group attributes if needed.
	$grouped = array();
	if ( 'yes' === $settings['group_by_group'] ) {
		foreach ( $attributes as $attr ) {
			$group_name = ! empty( $attr->group_name ) ? $attr->group_name : 'Other';
			if ( ! isset( $grouped[ $group_name ] ) ) {
				$grouped[ $group_name ] = array();
			}
			$grouped[ $group_name ][] = $attr;
		}
	} else {
		$grouped[''] = $attributes;
	}
	?>
	<div class="ggmc-custom-attributes layout-<?php echo esc_attr( $layout ); ?>" data-columns="<?php echo esc_attr( $columns ); ?>">
		<?php if ( ! empty( $settings['title'] ) ) : ?>
			<h3 class="ggmc-custom-attributes-title"><?php echo esc_html( $settings['title'] ); ?></h3>
		<?php endif; ?>

		<?php if ( empty( $attributes ) ) : ?>
			<p class="ggmc-no-attributes">No attributes available.</p>
		<?php else : ?>
			<?php foreach ( $grouped as $group_name => $group_attrs ) : ?>
				<?php if ( ! empty( $group_name ) && 'yes' === $settings['group_by_group'] ) : ?>
					<div class="ggmc-attr-group-header"><?php echo esc_html( $group_name ); ?></div>
				<?php endif; ?>

				<div class="ggmc-attr-list">
					<?php foreach ( $group_attrs as $attr ) : ?>
						<?php
						$has_icon  = 'yes' === $settings['show_icons'] && ! empty( $attr->icon );
						$icon_data = null;
						if ( $has_icon ) {
							// Try to parse as JSON (Elementor format).
							$icon_data = json_decode( $attr->icon, true );
							// If not JSON, treat as legacy class string.
							if ( ! is_array( $icon_data ) && ! empty( $attr->icon ) ) {
								$icon_data = array(
									'value'   => $attr->icon,
									'library' => 'fa-solid',
								);
							}
							// Validate we have a value.
							if ( empty( $icon_data['value'] ) ) {
								$has_icon  = false;
								$icon_data = null;
							}
						}
						?>
						<div class="ggmc-attr-row<?php echo $has_icon ? ' has-icon' : ''; ?>">
							<span class="ggmc-attr-name">
								<?php if ( $has_icon && $icon_data ) : ?>
									<span class="ggmc-attr-icon">
										<?php ggmc_render_icon( $icon_data, array( 'aria-hidden' => 'true' ) ); ?>
									</span>
								<?php endif; ?>
								<?php echo esc_html( $attr->name ); ?>
							</span>
							<span class="ggmc-attr-value">
								<?php echo esc_html( $attr->attribute_value ); ?>
								<?php if ( 'yes' === $settings['show_units'] && ! empty( $attr->unit ) ) : ?>
									<span class="ggmc-attr-unit"><?php echo esc_html( $attr->unit ); ?></span>
								<?php endif; ?>
							</span>
						</div>
					<?php endforeach; ?>
				</div>
			<?php endforeach; ?>
		<?php endif; ?>
	</div>
	<?php
}
