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

$cfg = bw_blog_sanitize_config( array(
	'per_page'   => $attributes['perPage'] ?? 8,
	'offset'     => $attributes['offset'] ?? 0,
	'categories' => $attributes['categories'] ?? array(),
	'tags'       => $attributes['tags'] ?? array(),
	'author'     => $attributes['author'] ?? 0,
	'sticky'     => $attributes['sticky'] ?? 'default',
	'ratio'      => $attributes['imageRatio'] ?? '66',
	'featured'   => ! empty( $attributes['featuredFirst'] ) ? 1 : 0,
) );

$heading = trim( (string) ( $attributes['heading'] ?? '' ) );
$columns = min( 6, max( 1, (int) ( $attributes['columns'] ?? 4 ) ) );

// Feature the newest post as a FIXED element above the grid; the grid then
// paginates the rest of the stream (offset + 1) so the feature never repeats.
// AJAX pagination swaps only the grid — the feature stays put on every page.
if ( $cfg['featured'] ) {
	$featured_post      = bw_blog_featured_post( $cfg, '' );
	$featured_html      = $featured_post ? bw_blog_render_featured_card( $featured_post, $cfg['ratio'] ) : '';
	$grid_cfg           = $cfg;
	$grid_cfg['offset'] = $cfg['offset'] + 1;
	$result             = bw_blog_query( $grid_cfg, 1, '' );
} else {
	$featured_html = '';
	$result        = bw_blog_query( $cfg, 1, '' );
}
$posts = $result['posts'];

// Optional vertical red rule at 33% across the feature (only meaningful with
// the feature on). Set on the wrapper so it survives AJAX card swaps.
$classes = 'bw-blog';
if ( $cfg['featured'] && ! empty( $attributes['featuredRule'] ) ) {
	$classes .= ' bw-blog--rule';
}

$wrapper = get_block_wrapper_attributes( array(
	'class' => $classes,
	'style' => '--bw-blog-cols:' . $columns,
) );

$search_svg = '<svg viewBox="0 0 512 512" aria-hidden="true" focusable="false" role="img" '
	. 'xmlns="http://www.w3.org/2000/svg"><path fill="currentColor" d="M416 208c0 45.9-14.9 88.3'
	. '-40 122.7L502.6 457.4c12.5 12.5 12.5 32.8 0 45.3s-32.8 12.5-45.3 0L330.7 376c-34.4 25.2'
	. '-76.8 40-122.7 40C93.1 416 0 322.9 0 208S93.1 0 208 0S416 93.1 416 208zM208 352a144 144 '
	. '0 1 0 0-288 144 144 0 1 0 0 288z"></path></svg>';

echo '<div ' . $wrapper
	. ' data-rest="' . esc_url( rest_url( 'brentwood/v1/blog-cards' ) ) . '"'
	. ' data-config="' . esc_attr( wp_json_encode( $cfg ) ) . '">';

echo '<div class="bw-blog__header">';
if ( '' !== $heading ) {
	echo '<h2 class="bw-blog__heading">' . esc_html( $heading ) . '</h2>';
}
if ( ! empty( $attributes['showSearch'] ) ) {
	echo '<div class="bw-blog__search">'
		. '<input type="search" placeholder="' . esc_attr__( 'Search posts…', 'kadence-child' ) . '" '
		. 'aria-label="' . esc_attr__( 'Search posts', 'kadence-child' ) . '">'
		. '<button type="button" class="bw-blog__search-toggle">' . $search_svg . ' '
		. esc_html__( 'Search', 'kadence-child' ) . '</button>'
		. '</div>';
}
echo '</div>';

// The feature is fixed across pages: AJAX pagination never touches it, only a
// new search refreshes it. CSS hides this wrapper when empty (feature off).
echo '<div class="bw-blog__featured">' . $featured_html . '</div>';

echo '<div class="bw-blog__grid" aria-live="polite">' . bw_blog_render_cards( $posts, $cfg['ratio'] ) . '</div>';

if ( ! empty( $attributes['showPagination'] ) ) {
	echo '<div class="bw-blog__pagination">' . bw_blog_render_paginator( $result['total_pages'], 1 ) . '</div>';
}

echo '</div>';
