<?php
if ( ! defined( 'ABSPATH' ) ) { exit; }
/**
 * Dynamic render for brentwood/media-grid.
 * Mirrors the Laravel photo-block / embed-video layout. $attributes provided by WP.
 *
 * @var array $attributes
 */
$a = $attributes;
$variant = $a['variant'] ?? 'grid';

if ($variant === 'video-hero') {
    $poster = $a['poster'] ?? '';
    $video  = $a['video'] ?? '';
    $head   = esc_html($a['header'] ?? '');
    $body   = wp_kses_post($a['body'] ?? '');
    if ($video) {
        $media = sprintf(
            '<video class="bw-hero__video" autoplay muted loop playsinline preload="metadata" poster="%s"><source src="%s" type="video/mp4"></video>',
            esc_url($poster), esc_url($video)
        );
    } else {
        $media = sprintf('<div class="bw-hero__video" style="background-image:url(\'%s\')"></div>', esc_url($poster));
    }
    echo '<div class="bw-hero"><div class="bw-hero__media">' . $media . '</div>'
        . '<div class="bw-hero__card">' . ($head ? '<h1>' . $head . '</h1>' : '') . $body . '</div></div>';
    return;
}

$cols   = (int) ($a['columns'] ?? 1);
$height = (int) ($a['height'] ?? 66);
$layout = $a['layout'] ?? 'grid';
$ts     = $a['textStyle'] ?? '';
$cls    = 'bw-mg bw-mg--' . esc_attr($layout) . ' bw-mg--cols' . $cols . ($ts ? ' bw-mg--' . esc_attr($ts) : '');

$cells = array();
foreach (($a['photos'] ?? array()) as $p) {
    $span  = max(1, (int) ($p['span'] ?? 1));
    $vspan = max(1, (int) ($p['vspan'] ?? 1));
    $pb    = max(25, min(200, (int) round($height / $span)));
    $cap   = (!empty($p['title']) || !empty($p['subtitle']))
        ? '<span class="bw-mg__cap"><strong>' . esc_html($p['title'] ?? '') . '</strong><em>' . esc_html($p['subtitle'] ?? '') . '</em></span>'
        : '';
    $media = '<div class="bw-mg__cell" style="--span:' . $span . ';--vspan:' . $vspan . ';--pb:' . $pb . '%">'
        . '<img loading="lazy" src="' . esc_url($p['url'] ?? '') . '" alt="' . esc_attr($p['alt'] ?? '') . '" '
        . 'style="object-position:' . esc_attr($p['focal'] ?? '50% 50%') . '">' . $cap . '</div>';
    if (!empty($p['link'])) {
        $media = '<a class="bw-mg__link" href="' . esc_url(home_url('/' . ltrim((string) $p['link'], '/'))) . '">' . $media . '</a>';
    }
    $cells[] = $media;
}

if (!empty($a['showText']) && (!empty($a['header']) || !empty($a['body']))) {
    $tspan = max(1, (int) ($a['textSpan'] ?? 1));
    $tcell = '<div class="bw-mg__text" style="--span:' . $tspan . '">'
        . (!empty($a['header']) ? '<h2>' . esc_html($a['header']) . '</h2>' : '')
        . wp_kses_post($a['body'] ?? '') . '</div>';
    $order = $a['textOrder'] ?? null;
    if (is_int($order) && $order >= 1 && $order <= count($cells) + 1) {
        array_splice($cells, $order - 1, 0, array($tcell));
    } else {
        array_unshift($cells, $tcell);
    }
}

$head_bar = ($layout === 'links' && !empty($a['header']))
    ? '<div class="bw-mg__head"><h3>' . esc_html($a['header']) . '</h3></div>' : '';

echo '<div class="' . esc_attr($cls) . '">' . $head_bar . '<div class="bw-mg__grid">' . implode('', $cells) . '</div></div>';
