<?php
declare(strict_types=1);

/**
 * /blocks/reclama-left.php
 *
 * Відповідає за:
 * - лівий боковий рекламний блок у стрічці
 * - показ тільки desktop-банера
 * - рендер окремим підключуваним файлом
 *
 * Очікує з батьківського файла:
 * - $sidebarBannerLeft
 * - $sidebarBannerLeftClickUrl
 * - $sidebarBannerRight
 * - $sidebarBannerRightClickUrl
 * - $T
 */

if (!function_exists('vm_side_ad_tower_assets')) {
    function vm_side_ad_tower_assets(): void
    {
        static $printed = false;
        if ($printed) {
            return;
        }
        $printed = true;
        ?>
        <style>
          .vm-side-ad-card.vm-side-ad-card--tower{
            padding:10px !important;
            border-radius:20px;
            overflow:hidden;
          }
          .vm-side-ad-card--tower .vm-side-ad-label{
            display:flex;
            align-items:center;
            justify-content:space-between;
            gap:10px;
            flex-wrap:wrap;
            margin-bottom:10px;
          }
          .vm-side-ad-card--tower .vm-side-ad-title{
            font-size:12px;
            font-weight:700;
            line-height:1.25;
            color:#495057;
            word-break:break-word;
            flex:1 1 100%;
          }
          .vm-side-ad-card--tower .vm-side-ad-thumb{
            display:block;
            width:100%;
            border-radius:16px;
            overflow:hidden;
            border:1px solid rgba(0,0,0,.08);
            background:linear-gradient(180deg,#0f172a,#111827);
          }
          .vm-side-ad-card--tower .vm-side-ad-thumb img{
            display:block;
            width:100%;
            height:100%;
            min-height:420px;
            max-height:min(72vh, 760px);
            object-fit:cover;
          }
          @media (min-width: 1400px){
            .vm-side-ad-card--tower .vm-side-ad-thumb img{
              min-height:520px;
            }
          }
          @media (max-width: 1199px){
            .vm-side-ad-card--tower .vm-side-ad-thumb img{
              min-height:360px;
            }
          }
          @media (max-width: 992px){
            .vm-desktop-ad{
              display:none !important;
            }
          }
        </style>
        <?php
    }
}

$banner = $sidebarBannerLeft ?? null;
$clickUrl = trim((string)($sidebarBannerLeftClickUrl ?? ''));

$image = is_array($banner) ? trim((string)($banner['desktop_banner_path'] ?? '')) : '';

if (!is_array($banner) || !$banner || $image === '') {
    return;
}

$title = trim((string)($banner['title'] ?? ''));
$label = isset($T) && is_callable($T)
    ? $T('Реклама', 'Реклама', 'Sponsored')
    : 'Sponsored';

$alt = $title !== '' ? $title : 'Advertisement';

vm_side_ad_tower_assets();
?>
<div class="card card-app mt-3 vm-side-ad-card vm-side-ad-card--tower vm-desktop-ad">
  <div class="vm-side-ad-label">
    <span class="vm-sponsored-badge"><?= h($label) ?></span>
    <?php if ($title !== ''): ?>
      <span class="vm-side-ad-title"><?= h($title) ?></span>
    <?php endif; ?>
  </div>

  <?php
    $campaignId = (int)($banner['id'] ?? 0);
    $openHref = ($clickUrl !== '' ? $clickUrl : '#');
    if ($campaignId > 0 && $clickUrl !== '') {
        $openHref = '/functional/reclama/click.php?c=' . $campaignId
            . '&p=' . rawurlencode('other')
            . '&to=' . rawurlencode($clickUrl);
    }
  ?>
  <a class="vm-side-ad-thumb"
     href="<?= h($openHref) ?>"
     target="_blank"
     rel="noopener nofollow sponsored">
    <img loading="lazy" decoding="async"
         src="<?= h($image) ?>"
         alt="<?= h($alt) ?>">
  </a>
</div>
