<?php
/**
 * Shared render template for the GoDAM Video Gallery V2.
 *
 * Used by:
 *   - assets/src/blocks/godam-gallery-v2/render.php    (Gutenberg block)
 *   - inc/classes/shortcodes/class-godam-video-gallery.php  (shortcode)
 *   - inc/classes/rest-api/class-dynamic-gallery.php   (only require_once'd for
 *     access to the helper functions below; never renders)
 *
 * Required call-site variables to render:
 *   $attributes              array  Block-shaped attributes (camelCase keys).
 *                                    When NOT set, the file only registers its
 *                                    helper functions and returns without output.
 *   $is_shortcode            bool   Optional; when true, build wrapper attributes
 *                                    manually instead of calling
 *                                    get_block_wrapper_attributes() (which only
 *                                    works inside a block render context).
 *   $inner_block_video_ids   int[]  Optional; pre-resolved video attachment IDs
 *                                    for the block's handpicked mode. Ignored
 *                                    when the resolved mode is 'query'.
 *
 * @package GoDAM
 */

defined( 'ABSPATH' ) || exit;

// -----------------------------------------------------------------------------
// Helper functions
//
// Declared at the top of the template so callers that only need the helpers
// (e.g. the gallery-shortcode REST endpoint) can `require_once` this file
// without triggering a render. The render section below is gated on
// $attributes being set.
// -----------------------------------------------------------------------------

if ( ! function_exists( 'rtgodam_gallery_v2_parse_id_list' ) ) {

	/**
	 * Parse a comma-separated ID list used by the godam/gallery-v2 surfaces.
	 *
	 * @param string $value Raw attribute value.
	 * @return int[]
	 */
	function rtgodam_gallery_v2_parse_id_list( $value ) {
		if ( ! is_string( $value ) || '' === trim( $value ) ) {
			return array();
		}

		$ids = array_map( 'trim', explode( ',', $value ) );
		$ids = array_map( 'absint', $ids );

		return array_values(
			array_filter(
				$ids,
				static fn( $id ) => $id > 0
			)
		);
	}
}

if ( ! function_exists( 'rtgodam_gallery_v2_get_thumbnail_url' ) ) {

	/**
	 * Resolve the best available thumbnail URL for a video attachment.
	 *
	 * @param int $attachment_id Attachment ID.
	 * @return string
	 */
	function rtgodam_gallery_v2_get_thumbnail_url( $attachment_id ) {
		$thumbnail_sources = rtgodam_get_video_thumbnail_sources( $attachment_id );

		return $thumbnail_sources['thumbnail'];
	}
}

if ( ! function_exists( 'rtgodam_gallery_v2_get_placeholder_thumbnail_url' ) ) {

	/**
	 * Resolve the blur-up placeholder URL for a video attachment.
	 *
	 * @param int $attachment_id Attachment ID.
	 * @return string
	 */
	function rtgodam_gallery_v2_get_placeholder_thumbnail_url( $attachment_id ) {
		$thumbnail_sources = rtgodam_get_video_thumbnail_sources( $attachment_id );

		return $thumbnail_sources['placeholder'];
	}
}

if ( ! function_exists( 'rtgodam_gallery_v2_format_display_date' ) ) {

	/**
	 * Format a post date for gallery output.
	 *
	 * @param string $date_string Raw date string.
	 * @return string
	 */
	function rtgodam_gallery_v2_format_display_date( $date_string ) {
		if ( empty( $date_string ) ) {
			return '';
		}

		$timestamp = strtotime( $date_string );

		return $timestamp ? wp_date( 'M j, Y', $timestamp ) : '';
	}
}

if ( ! function_exists( 'rtgodam_gallery_v2_get_relative_date' ) ) {

	/**
	 * Get a GMT datetime string for a relative number of days back from now.
	 *
	 * @param int $days Number of days back.
	 * @return string
	 */
	function rtgodam_gallery_v2_get_relative_date( $days ) {
		$timestamp = strtotime( '-' . absint( $days ) . ' days' );

		return $timestamp ? gmdate( 'Y-m-d H:i:s', $timestamp ) : '';
	}
}

if ( ! function_exists( 'rtgodam_gallery_v2_build_query_args' ) ) {

	/**
	 * Build attachment query args from godam/gallery-v2 block-shaped attributes.
	 *
	 * @param array $attributes Block-shaped attributes (camelCase keys).
	 * @param int   $page       Results page number.
	 * @return array
	 */
	function rtgodam_gallery_v2_build_query_args( $attributes, $page = 1 ) {
		$media_folder_ids = rtgodam_gallery_v2_parse_id_list( $attributes['mediaFolder'] ?? '' );
		$author_ids       = rtgodam_gallery_v2_parse_id_list( $attributes['author'] ?? '' );
		$count            = isset( $attributes['count'] ) ? max( 1, absint( $attributes['count'] ) ) : 6;
		$orderby          = isset( $attributes['orderby'] ) ? sanitize_key( $attributes['orderby'] ) : 'date';
		$order            = isset( $attributes['order'] ) ? strtoupper( sanitize_key( $attributes['order'] ) ) : 'DESC';
		$date_range       = isset( $attributes['dateRange'] ) ? sanitize_key( $attributes['dateRange'] ) : '';
		$date_query       = array();

		if ( '7days' === $date_range ) {
			$date_query[] = array( 'after' => rtgodam_gallery_v2_get_relative_date( 7 ) );
		} elseif ( '30days' === $date_range ) {
			$date_query[] = array( 'after' => rtgodam_gallery_v2_get_relative_date( 30 ) );
		} elseif ( '90days' === $date_range ) {
			$date_query[] = array( 'after' => rtgodam_gallery_v2_get_relative_date( 90 ) );
		} elseif ( 'custom' === $date_range ) {
			$after  = ! empty( $attributes['customDateStart'] ) ? gmdate( 'Y-m-d H:i:s', strtotime( $attributes['customDateStart'] ) ) : '';
			$before = ! empty( $attributes['customDateEnd'] ) ? gmdate( 'Y-m-d H:i:s', strtotime( $attributes['customDateEnd'] ) ) : '';

			if ( $after || $before ) {
				$date_filter = array();

				if ( $after ) {
					$date_filter['after'] = $after;
				}

				if ( $before ) {
					$date_filter['before'] = $before;
				}

				$date_query[] = $date_filter;
			}
		}

		$query_args = array(
			'post_type'              => 'attachment',
			'post_status'            => 'inherit',
			'post_mime_type'         => 'video',
			'posts_per_page'         => $count,
			'paged'                  => max( 1, absint( $page ) ),
			'orderby'                => in_array( $orderby, array( 'date', 'title' ), true ) ? $orderby : 'date',
			'order'                  => in_array( $order, array( 'ASC', 'DESC' ), true ) ? $order : 'DESC',
			'ignore_sticky_posts'    => true,
			'no_found_rows'          => false,
			'update_post_meta_cache' => true,
			'update_post_term_cache' => true,
		);

		if ( 'duration' === $orderby ) {
			// phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key -- Required for sorting by video duration.
			$query_args['meta_key'] = '_video_duration';
			$query_args['orderby']  = 'meta_value_num';
		} elseif ( 'size' === $orderby ) {
			// phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key -- Required for sorting by video file size.
			$query_args['meta_key'] = '_video_file_size';
			$query_args['orderby']  = 'meta_value_num';
		}

		if ( ! empty( $media_folder_ids ) ) {
			// phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_tax_query -- Tax query is necessary for folder filtering.
			$query_args['tax_query'] = array(
				array(
					'taxonomy' => 'media-folder',
					'field'    => 'term_id',
					'terms'    => $media_folder_ids,
				),
			);
		}

		if ( ! empty( $author_ids ) ) {
			$query_args['author__in'] = $author_ids;
		}

		if ( ! empty( $date_query ) ) {
			$query_args['date_query'] = $date_query;
		}

		return $query_args;
	}
}

if ( ! function_exists( 'rtgodam_gallery_v2_get_video_data' ) ) {

	/**
	 * Resolve normalized video card data for a gallery item.
	 *
	 * @param int $attachment_id Attachment ID.
	 * @return array|null
	 */
	function rtgodam_gallery_v2_get_video_data( $attachment_id ) {
		$attachment_id = absint( $attachment_id );
		$post          = $attachment_id ? get_post( $attachment_id ) : null;

		if ( ! $post || 'attachment' !== $post->post_type ) {
			return null;
		}

		// Defensive: drop non-video attachments. The block's MediaUpload, the
		// Elementor godam-media frame filter, and the shortcode's ids= attribute
		// can all in principle pass any attachment ID (manual JSON edit,
		// hand-written shortcode, third-party integration). Skip silently so
		// the rest of the picked list still renders.
		$mime = get_post_mime_type( $post );
		if ( ! is_string( $mime ) || 0 !== strpos( $mime, 'video/' ) ) {
			return null;
		}

		return array(
			'id'          => $attachment_id,
			'title'       => get_the_title( $attachment_id ) ?: __( 'Untitled video', 'godam' ),
			'date'        => rtgodam_gallery_v2_format_display_date( $post->post_date ),
			'thumbnail'   => rtgodam_gallery_v2_get_thumbnail_url( $attachment_id ),
			'placeholder' => rtgodam_gallery_v2_get_placeholder_thumbnail_url( $attachment_id ),
		);
	}
}

// -----------------------------------------------------------------------------
// Render
//
// Skipped when $attributes is not set so the file is safe to require_once
// purely for its helper functions.
// -----------------------------------------------------------------------------

if ( ! isset( $attributes ) || ! is_array( $attributes ) ) {
	return;
}

$godam_is_shortcode = isset( $is_shortcode ) && $is_shortcode;

$godam_gallery_mode        = isset( $attributes['mode'] ) ? sanitize_key( $attributes['mode'] ) : 'handpicked';
$godam_layout              = isset( $attributes['layout'] ) ? sanitize_key( $attributes['layout'] ) : 'carousel';
$godam_view_ratio          = isset( $attributes['viewRatio'] ) ? $attributes['viewRatio'] : '16:9';
$godam_allowed_ratios      = array( '16:9', '4:3', '9:16', '3:4', '1:1' );
$godam_view_ratio          = in_array( $godam_view_ratio, $godam_allowed_ratios, true ) ? $godam_view_ratio : '16:9';
$godam_item_width_size     = isset( $attributes['itemWidth'] ) ? $attributes['itemWidth'] : 'S';
$godam_item_width_map      = array(
	'S' => 200,
	'M' => 260,
	'L' => 320,
);
$godam_item_width          = isset( $godam_item_width_map[ $godam_item_width_size ] ) ? $godam_item_width_map[ $godam_item_width_size ] : 200;
$godam_show_title          = ! isset( $attributes['showTitle'] ) || (bool) $attributes['showTitle'];
$godam_performance_mode    = rtgodam_resolve_video_performance_mode( $attributes, 'balanced' );
$godam_enable_more_items   = array_key_exists( 'enableMoreItems', $attributes ) ? ! empty( $attributes['enableMoreItems'] ) : false;
$godam_more_items_behavior = isset( $attributes['moreItemsBehavior'] ) ? sanitize_key( $attributes['moreItemsBehavior'] ) : '';

if ( ! in_array( $godam_more_items_behavior, array( 'button', 'infinite' ), true ) ) {
	$godam_more_items_behavior = ! empty( $attributes['infiniteScroll'] ) ? 'infinite' : 'button';
}

if ( $godam_enable_more_items && 'carousel' === $godam_layout ) {
	$godam_more_items_behavior = 'infinite';
}

$godam_infinite_scroll       = $godam_enable_more_items && 'infinite' === $godam_more_items_behavior;
$godam_show_load_more_button = $godam_enable_more_items && 'button' === $godam_more_items_behavior;
$godam_ratio_class           = str_replace( ':', '-', $godam_view_ratio );
$godam_block_gap_raw         = $attributes['style']['spacing']['blockGap'] ?? '16px';
$godam_rest_query_args       = array();
$godam_total_query_items     = 0;

if ( is_string( $godam_block_gap_raw ) && 0 === strpos( $godam_block_gap_raw, 'var:preset|spacing|' ) ) {
	$godam_block_gap = 'var(--wp--preset--spacing--' . str_replace( 'var:preset|spacing|', '', $godam_block_gap_raw ) . ')';
} else {
	$godam_block_gap = $godam_block_gap_raw;
}

$godam_inline_styles = sprintf(
	'--godam-gallery-item-width: %dpx; --godam-gallery-gap: %s;',
	$godam_item_width,
	esc_attr( $godam_block_gap )
);

$godam_wrapper_attrs = array(
	'class'               => sprintf( 'godam-gallery-v2 godam-gallery-v2--%s', $godam_gallery_mode ),
	'style'               => $godam_inline_styles,
	'data-mode'           => $godam_gallery_mode,
	'data-layout'         => $godam_layout,
	'data-ratio'          => $godam_view_ratio,
	'data-embed-base-url' => home_url( '/' ),
	'data-engagements'    => rtgodam_is_engagement_feature_enabled() && ! empty( $attributes['engagements'] ) ? 'show' : '',
);

if ( $godam_is_shortcode ) {
	// get_block_wrapper_attributes() is only meaningful inside a block render
	// context; in the shortcode path we build the same attribute set manually.
	$godam_wrapper_parts = array();
	foreach ( $godam_wrapper_attrs as $godam_attr_key => $godam_attr_value ) {
		$godam_wrapper_parts[] = sprintf( '%s="%s"', $godam_attr_key, esc_attr( (string) $godam_attr_value ) );
	}
	$godam_wrapper_attributes = implode( ' ', $godam_wrapper_parts );
} else {
	$godam_wrapper_attributes = get_block_wrapper_attributes( $godam_wrapper_attrs );
}

$godam_items = array();

if ( 'query' === $godam_gallery_mode ) {
	$godam_rest_query_args   = array(
		'offset'            => 0,
		'columns'           => 1,
		'count'             => isset( $attributes['count'] ) ? max( 1, absint( $attributes['count'] ) ) : 6,
		'orderby'           => isset( $attributes['orderby'] ) ? sanitize_key( $attributes['orderby'] ) : 'date',
		'order'             => isset( $attributes['order'] ) ? strtoupper( sanitize_key( $attributes['order'] ) ) : 'DESC',
		'show_title'        => $godam_show_title ? '1' : '0',
		'layout'            => $godam_layout,
		'author'            => $attributes['author'] ?? '',
		'media_folder'      => $attributes['mediaFolder'] ?? '',
		'date_range'        => $attributes['dateRange'] ?? '',
		'custom_date_start' => $attributes['customDateStart'] ?? '',
		'custom_date_end'   => $attributes['customDateEnd'] ?? '',
		'gallery_variant'   => 'gallery-v2',
		'view_ratio'        => $godam_view_ratio,
		'performance_mode'  => $godam_performance_mode,
	);
	$godam_query             = new WP_Query( rtgodam_gallery_v2_build_query_args( $attributes, 1 ) );
	$godam_total_query_items = (int) $godam_query->found_posts;

	if ( $godam_query->have_posts() ) {
		foreach ( $godam_query->posts as $godam_video_post ) {
			$godam_item = rtgodam_gallery_v2_get_video_data( $godam_video_post->ID );

			if ( $godam_item ) {
				$godam_items[] = $godam_item;
			}
		}
	}

	wp_reset_postdata();
} elseif ( ! empty( $inner_block_video_ids ) && is_array( $inner_block_video_ids ) ) {
	foreach ( $inner_block_video_ids as $godam_video_id ) {
		$godam_item = rtgodam_gallery_v2_get_video_data( $godam_video_id );

		if ( $godam_item ) {
			$godam_items[] = $godam_item;
		}
	}
}

?>
<div <?php echo wp_kses_data( $godam_wrapper_attributes ); ?>>
	<div class="<?php echo esc_attr( sprintf( 'godam-gallery-v2__canvas godam-gallery-v2__canvas--%s', $godam_layout ) ); ?>">
		<?php if ( empty( $godam_items ) && 'query' === $godam_gallery_mode ) : ?>
			<div class="godam-gallery-v2__state">
				<strong><?php esc_html_e( 'No videos found', 'godam' ); ?></strong>
				<p><?php esc_html_e( 'Try changing the selected folder, author, or dates.', 'godam' ); ?></p>
			</div>
		<?php elseif ( 'query' === $godam_gallery_mode ) : ?>
			<div
				class="godam-gallery-v2__query-area"
				data-query-rest-url="<?php echo esc_url( rest_url( 'godam/v1/gallery-shortcode' ) ); ?>"
				data-query-args="<?php echo esc_attr( wp_json_encode( $godam_rest_query_args ) ); ?>"
				data-current-offset="<?php echo esc_attr( count( $godam_items ) ); ?>"
				data-total-items="<?php echo esc_attr( $godam_total_query_items ); ?>"
				data-enable-more-items="<?php echo $godam_enable_more_items ? 'true' : 'false'; ?>"
				data-more-items-behavior="<?php echo esc_attr( $godam_more_items_behavior ); ?>"
				data-infinite-scroll="<?php echo $godam_infinite_scroll ? 'true' : 'false'; ?>"
				data-show-title="<?php echo $godam_show_title ? 'true' : 'false'; ?>"
				data-view-ratio="<?php echo esc_attr( $godam_view_ratio ); ?>"
			>
			<div class="godam-gallery-v2__query-list">
				<?php foreach ( $godam_items as $godam_index => $godam_item ) : ?>
					<?php $godam_thumbnail_attributes = rtgodam_format_html_attributes( rtgodam_get_gallery_tile_image_attributes( $godam_performance_mode, $godam_index ) ); ?>
					<div class="<?php echo esc_attr( sprintf( 'godam-gallery-v2__query-item godam-gallery-v2__query-item--ratio-%s', $godam_ratio_class ) ); ?>">
						<button
							type="button"
							class="godam-gallery-v2__query-button"
							data-godam-gallery-v2-trigger="true"
							data-video-id="<?php echo esc_attr( $godam_item['id'] ); ?>"
							<?php /* translators: %s: video title. */ ?>
							aria-label="<?php echo esc_attr( sprintf( __( 'Open video: %s', 'godam' ), $godam_item['title'] ) ); ?>"
						>
							<div class="godam-gallery-v2__query-thumb<?php echo ! empty( $godam_item['placeholder'] ) ? ' godam-gallery-blurred-img' : ''; ?>"<?php echo ! empty( $godam_item['placeholder'] ) ? ' style="background-image: url(\'' . esc_url( $godam_item['placeholder'] ) . '\')"' : ''; ?>>
								<?php if ( ! empty( $godam_item['thumbnail'] ) ) : ?>
									<img src="<?php echo esc_url( $godam_item['thumbnail'] ); ?>" alt="<?php echo esc_attr( $godam_item['title'] ); ?>" class="godam-gallery-v2__thumbnail" <?php echo $godam_thumbnail_attributes ? wp_kses_data( $godam_thumbnail_attributes ) : ''; ?> />
								<?php else : ?>
									<span><?php esc_html_e( 'Video', 'godam' ); ?></span>
								<?php endif; ?>
							</div>
							<?php if ( $godam_show_title ) : ?>
								<div class="godam-gallery-v2__query-meta">
									<strong><?php echo esc_html( $godam_item['title'] ); ?></strong>
									<?php if ( ! empty( $godam_item['date'] ) ) : ?>
										<span><?php echo esc_html( $godam_item['date'] ); ?></span>
									<?php endif; ?>
								</div>
							<?php endif; ?>
						</button>
					</div>
				<?php endforeach; ?>
				<?php if ( $godam_total_query_items > count( $godam_items ) && $godam_show_load_more_button && 'carousel' === $godam_layout ) : ?>
					<div class="godam-gallery-v2__load-more-item">
						<button type="button" class="godam-gallery-v2__load-more wp-element-button">
							<?php esc_html_e( 'Load More', 'godam' ); ?>
						</button>
					</div>
				<?php endif; ?>
				<?php if ( $godam_infinite_scroll ) : ?>
					<div class="godam-gallery-v2__load-sentinel" aria-hidden="true"></div>
				<?php endif; ?>
			</div>
			<?php if ( $godam_total_query_items > count( $godam_items ) && $godam_show_load_more_button && 'carousel' !== $godam_layout ) : ?>
				<div class="godam-gallery-v2__load-more-wrap">
					<button type="button" class="godam-gallery-v2__load-more wp-element-button">
						<?php esc_html_e( 'Load More', 'godam' ); ?>
					</button>
				</div>
			<?php endif; ?>
			</div>
		<?php else : ?>
			<div class="godam-gallery-v2__item-list">
				<?php foreach ( $godam_items as $godam_index => $godam_item ) : ?>
					<?php $godam_thumbnail_attributes = rtgodam_format_html_attributes( rtgodam_get_gallery_tile_image_attributes( $godam_performance_mode, $godam_index ) ); ?>
					<div class="<?php echo esc_attr( sprintf( 'godam-gallery-v2-item godam-gallery-v2-item--%s godam-gallery-v2-item--ratio-%s', $godam_layout, $godam_ratio_class ) ); ?>">
						<button
							type="button"
							class="godam-gallery-v2-item__button"
							data-godam-gallery-v2-trigger="true"
							data-video-id="<?php echo esc_attr( $godam_item['id'] ); ?>"
							<?php /* translators: %s: video title. */ ?>
							aria-label="<?php echo esc_attr( sprintf( __( 'Open video: %s', 'godam' ), $godam_item['title'] ) ); ?>"
						>
							<div class="godam-gallery-v2-item__preview<?php echo ! empty( $godam_item['placeholder'] ) ? ' godam-gallery-blurred-img' : ''; ?>"<?php echo ! empty( $godam_item['placeholder'] ) ? ' style="background-image: url(\'' . esc_url( $godam_item['placeholder'] ) . '\')"' : ''; ?>>
								<?php if ( ! empty( $godam_item['thumbnail'] ) ) : ?>
									<img
										src="<?php echo esc_url( $godam_item['thumbnail'] ); ?>"
										alt="<?php echo esc_attr( $godam_item['title'] ); ?>"
										class="godam-gallery-v2-item__thumbnail godam-gallery-v2__thumbnail"
										<?php echo $godam_thumbnail_attributes ? wp_kses_data( $godam_thumbnail_attributes ) : ''; ?>
									/>
								<?php else : ?>
									<div class="godam-gallery-v2-item__placeholder">
										<span><?php esc_html_e( 'Video', 'godam' ); ?></span>
									</div>
								<?php endif; ?>
								<div class="godam-gallery-v2-item__play-icon" aria-hidden="true">
									<svg viewBox="0 0 24 24" fill="currentColor">
										<path d="M8 5v14l11-7z" />
									</svg>
								</div>
							</div>
							<?php if ( $godam_show_title ) : ?>
								<div class="godam-gallery-v2-item__meta">
									<div class="godam-gallery-v2-item__copy">
										<strong title="<?php echo esc_attr( $godam_item['title'] ); ?>"><?php echo esc_html( $godam_item['title'] ); ?></strong>
										<?php if ( ! empty( $godam_item['date'] ) ) : ?>
											<span><?php echo esc_html( $godam_item['date'] ); ?></span>
										<?php endif; ?>
									</div>
								</div>
							<?php endif; ?>
						</button>
					</div>
				<?php endforeach; ?>
			</div>
		<?php endif; ?>
	</div>
</div>
