<?php
declare(strict_types=1);

require_once __DIR__ . '/../../includes/start.php';
require_once __DIR__ . '/_lib.php';

if (!function_exists('vm_site_pages_view_excerpt')) {
    function vm_site_pages_view_excerpt(string $text, int $limit = 180): string
    {
        $text = trim(preg_replace('~\s+~u', ' ', strip_tags($text)) ?? '');
        if ($text === '') {
            return '';
        }

        if (function_exists('mb_strlen') && function_exists('mb_substr')) {
            if (mb_strlen($text, 'UTF-8') <= $limit) {
                return $text;
            }
            return rtrim(mb_substr($text, 0, $limit, 'UTF-8')) . '…';
        }

        if (strlen($text) <= $limit) {
            return $text;
        }

        return rtrim(substr($text, 0, $limit)) . '…';
    }
}

$pdo = vm_site_pages_db();
$slugRaw = (string)($_GET['slug'] ?? '');
$slug = vm_site_pages_slugify($slugRaw);

$row = vm_site_pages_find_by_slug($pdo, $slug, true);

if (!$row) {
    http_response_code(404);
    if (function_exists('seo_noindex')) {
        seo_noindex(true);
    }
    if (function_exists('seo_set')) {
        seo_set([
            'title' => '404 — VMEREGI',
            'description' => '',
        ]);
    }

    require __DIR__ . '/../../includes/header.php';
    ?>
    <div class="container py-4">
      <div class="card shadow-sm border-0">
        <div class="card-body p-4">
          <h1 class="h4 mb-3">404</h1>
          <div class="text-muted">
            <?php
            $langNow = vm_site_pages_current_lang();
            echo vm_site_pages_h(match ($langNow) {
                'ru' => 'Страница не найдена.',
                'en' => 'Page not found.',
                default => 'Сторінку не знайдено.',
            });
            ?>
          </div>
        </div>
      </div>
    </div>
    <?php
    require __DIR__ . '/../../includes/footer.php';
    exit;
}

$langNow = vm_site_pages_current_lang();
$pageTitle = vm_site_pages_row_title($row, $langNow);
$pageBody = vm_site_pages_row_body($row, $langNow);
$pageDescription = vm_site_pages_view_excerpt($pageBody, 180);

if (function_exists('seo_set')) {
    $canonical = '/functional/pages/view.php?slug=' . rawurlencode((string)$row['slug']);

    seo_set([
        'title' => $pageTitle !== '' ? $pageTitle . ' — VMEREGI' : 'VMEREGI',
        'description' => $pageDescription,
        'canonical' => $canonical,
        'type' => 'article',
    ]);
}

require __DIR__ . '/../../includes/header.php';
?>
<style>
  .vm-legal-wrap {
    max-width: 920px;
    margin: 0 auto;
  }

  .vm-legal-card {
    border: 0;
    border-radius: 20px;
    box-shadow: 0 8px 28px rgba(0, 0, 0, .06);
  }

  .vm-legal-body {
    font-size: 16px;
    line-height: 1.75;
    color: #222;
    white-space: normal;
    word-wrap: break-word;
  }

  .vm-legal-body p:last-child {
    margin-bottom: 0;
  }
</style>

<div class="container py-4">
  <div class="vm-legal-wrap">
    <div class="card vm-legal-card">
      <div class="card-body p-4 p-md-5">
        <h1 class="h3 mb-4"><?= vm_site_pages_h($pageTitle) ?></h1>
        <div class="vm-legal-body">
          <?= nl2br(vm_site_pages_h($pageBody)) ?>
        </div>
      </div>
    </div>
  </div>
</div>

<?php require __DIR__ . '/../../includes/footer.php'; ?>