<?php
require_once __DIR__ . '/inc/template.php';
require_once __DIR__ . '/inc/data.php';

$slug = isset($_GET['slug']) ? preg_replace('/[^a-z0-9-]/','',$_GET['slug']) : '';
$a = $slug ? article_par_slug($slug) : null;

if (!$a) {
    http_response_code(404);
    page_title('Article introuvable');
    ob_start(); ?>
    <section class="wrap" style="padding:6rem 2rem;text-align:center">
      <h1 class="cor" style="font-size:2rem">Article introuvable</h1>
      <p style="color:var(--ink2);margin-top:1rem">Cet article n'existe pas ou n'est plus disponible.</p>
      <p style="margin-top:1.5rem"><a class="btn btn-blue" href="<?= url_articles() ?>">Voir tous les articles</a></p>
    </section>
    <?php
    render_page(ob_get_clean());
    exit;
}

page_active('articles');
page_title($a['meta_titre'] ?: $a['titre']);
page_desc($a['meta_desc'] ?: extrait_texte($a['contenu'], 155));
$imgAbs = !empty($a['image_url']) ? SITE_URL . '/' . ltrim($a['image_url'],'/') : '';
if ($imgAbs) page_og_image($imgAbs);

$dateIso  = !empty($a['cree_le'])   ? date('c', strtotime($a['cree_le']))   : date('c');
$dateModIso = !empty($a['modifie_le']) ? date('c', strtotime($a['modifie_le'])) : $dateIso;
$dateFr   = !empty($a['cree_le'])   ? date('d/m/Y', strtotime($a['cree_le'])) : '';
$urlAbs   = url_article($a['slug']);

$lies = articles_lies((int)$a['id'], !empty($a['categorie_id']) ? (int)$a['categorie_id'] : null, 4);

ob_start();
?>
<article>
  <section class="page-hero<?= $imgAbs ? ' page-hero-article' : '' ?>">
    <div class="grain"></div>
    <div class="wrap">
      <div class="hero-art-txt">
        <?= fil_ariane([['Articles', url_articles()], [$a['titre'], null]]) ?>
        <?php if (!empty($a['cat_nom'])): ?><div class="kick tag"><?= e($a['cat_nom']) ?></div><?php endif; ?>
        <h1 class="cor"><?= e($a['titre']) ?></h1>
        <?php if ($dateFr): ?><p class="lead" style="font-size:1rem;opacity:.8">Publié le <?= e($dateFr) ?></p><?php endif; ?>
      </div>
      <?php if ($imgAbs): ?>
        <div class="hero-art-img">
          <div class="zone-img ronde fondu" style="aspect-ratio:1/1">
            <img src="/<?= e(ltrim($a['image_url'],'/')) ?>" alt="<?= e($a['titre']) ?>">
          </div>
        </div>
      <?php endif; ?>
    </div>
  </section>

  <section class="wrap prose" style="max-width:780px">
    <?php if (function_exists('admin_connecte') && admin_connecte()): ?>
      <a class="edit-article-btn" href="/admin/article_edit.php?id=<?= (int)$a['id'] ?>">✎ Modifier cet article</a>
    <?php endif; ?>

    <div class="article-contenu">
      <?= $a['contenu'] /* HTML déjà nettoyé à la saisie admin */ ?>
    </div>

    <div class="cta-line">
      <a href="<?= url_articles() ?>" class="btn btn-o-d">← Tous les articles</a>
      <a href="/contact.php" class="btn btn-blue">Prendre rendez-vous</a>
    </div>
  </section>

  <?php if ($lies): ?>
  <section class="wrap articles-lies">
    <h2 class="cor section-t">À lire aussi</h2>
    <div class="lies-grid">
      <?php foreach ($lies as $l): $ext = !empty($l['externe']); ?>
        <a class="lie-card" href="<?= e($l['lien']) ?>"<?= $ext ? ' target="_blank" rel="noopener"' : '' ?>>
          <?php if (!empty($l['image_url'])): ?>
            <div class="lie-img"><img src="<?= e($l['image_url']) ?>" alt="<?= e($l['titre']) ?>" loading="lazy"></div>
          <?php else: ?>
            <div class="lie-img lie-img-vide"><span class="cor">推拿</span></div>
          <?php endif; ?>
          <div class="lie-body">
            <?php if (!empty($l['cat_nom'])): ?><div class="lie-cat"><?= e($l['cat_nom']) ?></div><?php endif; ?>
            <h3 class="cor"><?= e($l['titre']) ?></h3>
            <p><?= e($l['extrait'] ?: extrait_texte($l['contenu'], 100)) ?></p>
          </div>
        </a>
      <?php endforeach; ?>
    </div>
  </section>
  <?php endif; ?>
</article>

<script type="application/ld+json">
<?= json_encode([
  '@context' => 'https://schema.org',
  '@type' => 'Article',
  'headline' => $a['titre'],
  'description' => $a['meta_desc'] ?: extrait_texte($a['contenu'], 155),
  'datePublished' => $dateIso,
  'dateModified' => $dateModIso,
  'image' => $imgAbs ?: null,
  'inLanguage' => 'fr-FR',
  'mainEntityOfPage' => ['@type'=>'WebPage','@id'=>$urlAbs],
  'url' => $urlAbs,
  'author' => ['@type'=>'Person','name'=>'Hérald Loygue','url'=>SITE_URL.'/le-praticien.php'],
  'publisher' => [
    '@type'=>'Organization','name'=>'Tuina Loygue',
    'logo'=>['@type'=>'ImageObject','url'=>SITE_URL.'/assets/logo.png'],
  ],
  'articleSection' => $a['cat_nom'] ?? null,
], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT) ?>
</script>
<?php
render_page(ob_get_clean());