<?php
/**
 * Kit Contents Widget - Render Methods
 *
 * @package GGM_Commerce
 */

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

/**
 * Render the premium widget content.
 *
 * @param GGM_Commerce_Widget_Kit_Contents $widget Widget instance.
 */
function ggmc_kit_contents_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 kit and components.
	$kits_manager = GGM_Commerce_Kits::get_instance();
	$kit          = null;
	$components   = array();

	if ( $product_id ) {
		$kit = $kits_manager->get_kit_by_product_id( $product_id );
		if ( $kit ) {
			$components = $kits_manager->get_kit_components( $kit->id );
		}
	}

	// In editor, show placeholder if no components.
	if ( $is_editor && empty( $components ) ) {
		$components = array(
			(object) array(
				'component_product_id' => 1,
				'product_name'         => 'Sample Component A',
				'sku'                  => 'COMP-A',
				'price'                => 19.99,
				'quantity'             => 2,
				'image_id'             => 0,
			),
			(object) array(
				'component_product_id' => 2,
				'product_name'         => 'Sample Component B',
				'sku'                  => 'COMP-B',
				'price'                => 14.99,
				'quantity'             => 1,
				'image_id'             => 0,
			),
			(object) array(
				'component_product_id' => 3,
				'product_name'         => 'Sample Component C',
				'sku'                  => 'COMP-C',
				'price'                => 9.99,
				'quantity'             => 3,
				'image_id'             => 0,
			),
		);
	}

	// Hide if not a kit/build and setting enabled.
	if ( empty( $components ) && 'yes' === $settings['hide_if_not_kit'] ) {
		return;
	}

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

		<?php if ( empty( $components ) ) : ?>
			<p class="ggmc-no-components">This product has no components.</p>
		<?php elseif ( 'table' === $layout ) : ?>
			<table class="ggmc-components-table">
				<thead>
					<tr>
						<?php if ( 'yes' === $settings['show_images'] ) : ?>
							<th class="col-image"></th>
						<?php endif; ?>
						<th class="col-name">Item</th>
						<?php if ( 'yes' === $settings['show_quantities'] ) : ?>
							<th class="col-qty">Qty</th>
						<?php endif; ?>
						<?php if ( 'yes' === $settings['show_prices'] ) : ?>
							<th class="col-price">Price</th>
						<?php endif; ?>
					</tr>
				</thead>
				<tbody>
					<?php foreach ( $components as $component ) : ?>
						<tr class="ggmc-component-item">
							<?php if ( 'yes' === $settings['show_images'] ) : ?>
								<td class="ggmc-component-image">
									<?php echo wp_kses_post( ggmc_kit_contents_get_component_image( $component ) ); ?>
								</td>
							<?php endif; ?>
							<td class="ggmc-component-name">
								<?php echo wp_kses_post( ggmc_kit_contents_get_component_name( $component, $settings ) ); ?>
							</td>
							<?php if ( 'yes' === $settings['show_quantities'] ) : ?>
								<td class="ggmc-component-qty"><?php echo esc_html( $component->quantity ); ?>x</td>
							<?php endif; ?>
							<?php if ( 'yes' === $settings['show_prices'] ) : ?>
								<td class="ggmc-component-price">$<?php echo esc_html( number_format( (float) $component->price, 2 ) ); ?></td>
							<?php endif; ?>
						</tr>
					<?php endforeach; ?>
				</tbody>
			</table>
		<?php else : ?>
			<div class="ggmc-components-<?php echo esc_attr( $layout ); ?>">
				<?php foreach ( $components as $component ) : ?>
					<div class="ggmc-component-item">
						<?php if ( 'yes' === $settings['show_images'] ) : ?>
							<div class="ggmc-component-image">
								<?php echo wp_kses_post( ggmc_kit_contents_get_component_image( $component ) ); ?>
							</div>
						<?php endif; ?>
						<div class="ggmc-component-details">
							<span class="ggmc-component-name">
								<?php echo wp_kses_post( ggmc_kit_contents_get_component_name( $component, $settings ) ); ?>
							</span>
							<?php if ( 'yes' === $settings['show_prices'] ) : ?>
								<span class="ggmc-component-price">$<?php echo esc_html( number_format( (float) $component->price, 2 ) ); ?></span>
							<?php endif; ?>
						</div>
						<?php if ( 'yes' === $settings['show_quantities'] ) : ?>
							<span class="ggmc-component-qty"><?php echo esc_html( $component->quantity ); ?></span>
						<?php endif; ?>
					</div>
				<?php endforeach; ?>
			</div>
		<?php endif; ?>
	</div>
	<?php
}

/**
 * Get component image HTML.
 *
 * @param object $component Component object.
 * @return string HTML for image.
 */
function ggmc_kit_contents_get_component_image( $component ) {
	$image_id = isset( $component->image_id ) ? (int) $component->image_id : 0;

	if ( $image_id > 0 ) {
		$image = wp_get_attachment_image( $image_id, 'thumbnail' );
		if ( $image ) {
			return $image;
		}
	}

	// Placeholder.
	return '<img src="' . esc_url( plugin_dir_url( __FILE__ ) . '../../../assets/images/placeholder.svg' ) . '" alt="Placeholder" />';
}

/**
 * Get component name HTML with optional link.
 *
 * @param object $component Component object.
 * @param array  $settings  Widget settings.
 * @return string HTML for name.
 */
function ggmc_kit_contents_get_component_name( $component, $settings ) {
	$name = esc_html( $component->product_name );

	if ( 'yes' === $settings['link_to_products'] && ! empty( $component->component_product_id ) ) {
		$url = GGM_Commerce_Helpers::get_product_details_url( $component->component_product_id );
		if ( $url ) {
			return '<a href="' . esc_url( $url ) . '">' . $name . '</a>';
		}
	}

	return $name;
}
