<?php
if ( ! defined( 'ABSPATH' ) ) { exit; }
/**
 * Dynamic render for bw/youtube-list — initial page-1 markup. The view script
 * (view.js) then drives search + pagination through the
 * brentwood/v1/livestream-cards REST endpoint, swapping rows in place.
 *
 * @var array $attributes
 */
require_once __DIR__ . '/helpers.php';

$cfg = bw_yt_sanitize_config( array(
	'per_page'   => $attributes['perPage'] ?? 10,
	'scope'      => $attributes['scope'] ?? 'completed',
	'categories' => $attributes['categories'] ?? array(),
	'exclude'    => $attributes['exclude'] ?? array(),
	'show_unlisted' => $attributes['showUnlisted'] ?? false,
) );

$result  = bw_yt_query( $cfg, 1, '' );
$heading = trim( (string) ( $attributes['heading'] ?? '' ) );

$wrapper = get_block_wrapper_attributes( array( 'class' => 'bw-yt' ) );

echo '<div ' . $wrapper
	. ' data-rest="' . esc_url( rest_url( 'brentwood/v1/livestream-cards' ) ) . '"'
	. ' data-config="' . esc_attr( wp_json_encode( $cfg ) ) . '"'
	. ' data-site-tz="' . esc_attr( wp_timezone_string() ) . '"'
	. ' data-site-label="Brentwood">';

echo '<div class="bw-yt__header">';
if ( '' !== $heading ) {
	echo '<h2 class="bw-yt__heading">' . esc_html( $heading ) . '</h2>';
}
// Right-hand column: brand icon, then the timezone switcher, then search —
// stacked top-to-bottom and right-aligned.
echo '<div class="bw-yt__header-right">';
// Top row: timezone switcher bar sits inline with the brand icon, centered.
// The tzbar is filled by view.js (needs the viewer's timezone); it stays empty
// (and hidden) without JS, in which case the icon is alone on the row.
echo '<div class="bw-yt__brandrow">';
echo '<div class="bw-yt__tzbar"></div>';
echo '<span class="bw-yt__brand" aria-hidden="true">' . bw_yt_icon( 'youtube' ) . '</span>';
echo '</div>'; // .bw-yt__brandrow
if ( ! empty( $attributes['showSearch'] ) ) {
	echo '<div class="bw-yt__search">'
		. '<span class="bw-yt__search-icon" aria-hidden="true">' . bw_yt_icon( 'search' ) . '</span>'
		. '<input type="search" placeholder="' . esc_attr__( 'Search…', 'kadence-child' ) . '" '
		. 'aria-label="' . esc_attr__( 'Search events', 'kadence-child' ) . '">'
		. '</div>';
}
echo '</div>'; // .bw-yt__header-right
echo '</div>';

echo '<div class="bw-yt__list" aria-live="polite">' . bw_yt_render_rows( $result['posts'] ) . '</div>';

echo '<div class="bw-yt__footer">';
echo '<div class="bw-yt__count">' . bw_yt_render_count( $result['found'] ) . '</div>';
if ( ! empty( $attributes['showPagination'] ) ) {
	echo '<div class="bw-yt__pagination">' . bw_yt_render_paginator( $result['total_pages'], 1 ) . '</div>';
}
echo '</div>';

echo '</div>';
