<?php
if ( ! defined( 'ABSPATH' ) ) { exit; }
/**
 * Dynamic render for brentwood/hero-card — the white hero text card as a
 * block. The H1 prefers the bw-dev "Override H1 page title" (post meta
 * _bw_dev_title_override, which allows limited inline HTML) and falls back to
 * the default WordPress page title when the override is empty. The editable
 * body is InnerBlocks ($content). The hero MEDIA comes from the page template
 * (inc/brentwood-hero.php), which renders media-only when this block is present.
 *
 * @var array  $attributes
 * @var string $content  Rendered inner blocks.
 */
$show_title    = ! isset( $attributes['showTitle'] ) || $attributes['showTitle'];
$align_ok      = array( 'left', 'center', 'right' );
$title_align   = isset( $attributes['titleAlign'] ) && in_array( $attributes['titleAlign'], $align_ok, true ) ? $attributes['titleAlign'] : 'left';
$content_align = isset( $attributes['contentAlign'] ) && in_array( $attributes['contentAlign'], $align_ok, true ) ? $attributes['contentAlign'] : 'left';
$dash_align    = isset( $attributes['dashAlign'] ) && in_array( $attributes['dashAlign'], $align_ok, true ) ? $attributes['dashAlign'] : 'left';

// H1: use the bw-dev Title Override when set (it stores kses-cleaned inline
// HTML — <em>/<strong>/<i>/<b>/<br>/<span class>), otherwise the page title.
$heading = '';
if ( $show_title ) {
	$override = get_post_meta( get_the_ID(), '_bw_dev_title_override', true );
	if ( is_string( $override ) && '' !== trim( $override ) ) {
		$heading = wp_kses(
			$override,
			array(
				'em'     => array(),
				'strong' => array(),
				'i'      => array(),
				'b'      => array(),
				'br'     => array(),
				'span'   => array( 'class' => array() ),
			)
		);
	} else {
		$heading = esc_html( get_the_title() );
	}
}

// The closing dash — the saved child-theme dash.svg (matches the live site's
// /images/dash.svg), aligned per the block setting.
$dash = '<img class="bw-phero__dash bw-phero__dash--' . esc_attr( $dash_align ) . '" src="'
	. esc_url( get_stylesheet_directory_uri() . '/assets/dash.svg' ) . '" alt="" aria-hidden="true">';

// Optional call-to-action button — the brand-red link button shown at the
// top-right of the card, beside the heading (mirrors the live "Apply Now ›").
// Just text + link; styling is fixed (brand red) so editors set no values.
$cta_html = '';
if ( ! empty( $attributes['ctaEnabled'] ) ) {
	$cta_text = isset( $attributes['ctaText'] ) ? trim( (string) $attributes['ctaText'] ) : '';
	if ( '' !== $cta_text ) {
		$cta_url    = isset( $attributes['ctaUrl'] ) ? trim( (string) $attributes['ctaUrl'] ) : '';
		$href       = '' !== $cta_url ? esc_url( $cta_url ) : '#';
		$new_tab    = ! empty( $attributes['ctaNewTab'] );
		$rel_target = $new_tab ? ' target="_blank" rel="noopener noreferrer"' : '';
		$cta_html   = '<a class="bw-hero-cta" href="' . $href . '"' . $rel_target . '>'
			. '<span class="bw-hero-cta__label">' . esc_html( $cta_text ) . '</span>'
			. '<span class="bw-hero-cta__arrow" aria-hidden="true">&rsaquo;</span>'
			. '</a>';
	}
}

$title_html = $show_title ? '<h1 style="text-align:' . esc_attr( $title_align ) . '">' . $heading . '</h1>' : '';

// With a CTA, the title + button share a flex header row so the button sits at
// the top-right of the card. Without one, the title renders exactly as before.
$head_html = ( '' !== $cta_html )
	? '<div class="bw-phero__head">' . $title_html . $cta_html . '</div>'
	: $title_html;

// "Play Video" trigger — shown only when THIS page's hero is a Featured YouTube
// (mirrors brentwood.ca). Clicking it plays the hero video inline: page-hero.js
// wires .bw-hero-playvideo to the hero's YouTube facade, which also drops this
// card below the video while it plays.
$play_video_html = '';
if ( function_exists( 'get_field' ) && function_exists( 'bw_youtube_id' ) ) {
	$pid = get_the_ID();
	if ( 'youtube' === (string) get_field( 'hero_type', $pid )
		&& '' !== bw_youtube_id( (string) get_field( 'hero_youtube_id', $pid ) ) ) {
		$play_video_html = '<button type="button" class="bw-hero-playvideo">'
			. '<svg class="bw-hero-playvideo__icon" viewBox="0 0 28 20" aria-hidden="true">'
			. '<path fill="#f00" d="M27.4 3.12a3.52 3.52 0 0 0-2.48-2.49C22.74 0 14 0 14 0S5.26 0 3.08.63A3.52 3.52 0 0 0 .6 3.12 36.7 36.7 0 0 0 0 10a36.7 36.7 0 0 0 .6 6.88 3.52 3.52 0 0 0 2.48 2.49C5.26 20 14 20 14 20s8.74 0 10.92-.63a3.52 3.52 0 0 0 2.48-2.49A36.7 36.7 0 0 0 28 10a36.7 36.7 0 0 0-.6-6.88z"/>'
			. '<path fill="#fff" d="M11.2 14.29 18.78 10 11.2 5.71z"/></svg>'
			. '<span>' . esc_html__( 'Play Video', 'kadence-child' ) . '</span>'
			. '</button>';
	}
}

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

echo '<div ' . $wrapper . '><div class="bw-phero__cardwrap"><div class="bw-phero__card">'
	. $head_html
	. '<div class="bw-phero__body" style="text-align:' . esc_attr( $content_align ) . '">' . $content . '</div>'
	. $play_video_html
	. $dash
	. '</div></div></div>';
