<?php

/**

 * Magellio News — article page v2 (MD11 Phase B).

 */

require_once __DIR__ . '/includes/frontend/layout.php';

require_once __DIR__ . '/includes/seo/SchemaService.php';

news_front_bootstrap();



$slug = trim((string) ($_GET['slug'] ?? ''));

$reqCategory = strtolower(trim((string) ($_GET['category'] ?? '')));

$article = $slug !== '' ? NewsPublicRepository::findBySlug($slug) : null;

if (!$article) {

	http_response_code(404);

	news_front_layout_start([

		'title' => 'Article introuvable — Magellio News',

		'description' => 'Cet article n’est plus disponible.',

		'canonical' => news_front_absolute_url(),

		'body_class' => 'mn-article-page mn-front-md11',

	]);

	news_front_not_found_block('Article introuvable', 'Cet article n’est plus disponible ou l’adresse est incorrecte.');

	news_front_layout_end();

	exit;

}



$canonicalPath = news_article_public_path($article);

$theme = news_article_theme_slug((string) ($article['category_slug'] ?? ''));

if ($reqCategory === '' || $reqCategory !== $theme) {

	header('Location: ' . news_front_absolute_url($canonicalPath), true, 301);

	exit;

}



NewsPublicRepository::ensureReadingTime($article);

$sources = NewsPublicRepository::articleSources((int) $article['id']);

$related = NewsPublicRepository::related($article, 4);

$image = NewsPublicRepository::imagePublicUrls($article);

$heroLandscape = $image['landscape_medium'] ?: $image['landscape_small'];

$heroPortrait = $image['portrait_medium'] ?: $image['portrait_small'] ?: $heroLandscape;

$heroImg = $heroLandscape;

$socialImg = ($image['social'] ?? '') !== '' ? $image['social'] : $heroImg;

$cats = news_front_categories();

$themeSlug = $theme;

$catLabel = $cats[$themeSlug] ?? ($themeSlug !== '' ? $themeSlug : 'Analyses');

$title = (string) ($article['seo_title'] ?: $article['h1']);

$desc = (string) ($article['meta_description'] ?: $article['standfirst'] ?: '');

$canonical = news_front_absolute_url($canonicalPath);

$og = $socialImg !== '' ? news_front_absolute_url($socialImg) : '';

$author = (string) ($article['persona_name'] ?? 'La rédaction Magellio News');

$authorSlug = (string) ($article['persona_slug'] ?? '');

$reading = max(1, (int) ($article['reading_time_minutes'] ?? 1));

$updated = (string) ($article['updated_at'] ?? '');

$published = (string) ($article['published_at'] ?? '');



$jsonLd = NewsSchemaService::buildForPage('article', [

	'article' => $article,

	'canonical' => $canonical,

	'description' => $desc,

	'image' => $og,

	'author_name' => $author,

	'author_slug' => $authorSlug,

]);



news_front_layout_start([

	'title' => $title,

	'description' => $desc,

	'canonical' => $canonical,

	'og_image' => $og,

	'og_type' => 'article',

	'active_nav' => $themeSlug !== '' ? $themeSlug : 'une',

	'body_class' => 'mn-article-page mn-front-md11',

	'json_ld' => $jsonLd,

]);

?>



<div class="mn-article-shell">

	<article class="mn-article">

		<header class="mn-article__header mn-article__header--narrow">

			<nav class="mn-breadcrumb" aria-label="Fil d’Ariane">

				<a href="<?php echo news_h(news_url()); ?>">Actualités</a>

				<span aria-hidden="true">/</span>

				<a href="<?php echo news_h(news_url($themeSlug . '/')); ?>"><?php echo news_h($catLabel); ?></a>

			</nav>

			<?php if ($catLabel !== ''): ?><span class="mn-cat"><?php echo news_h($catLabel); ?></span><?php endif; ?>

			<h1 class="mn-article__title"><?php echo news_h((string) $article['h1']); ?></h1>

			<?php if (!empty($article['standfirst'])): ?>

				<p class="mn-article__standfirst"><?php echo news_h((string) $article['standfirst']); ?></p>

			<?php endif; ?>

			<div class="mn-article__byline">

				<p class="mn-meta mn-meta--with-avatar">

					<?php news_front_persona_avatar($article, 'mn-meta__avatar', 28, $author); ?>

					<span>

						Par <?php if ($authorSlug !== ''): ?><a href="<?php echo news_h(news_url('equipe/' . $authorSlug . '.html')); ?>"><?php echo news_h($author); ?></a><?php else: ?><?php echo news_h($author); ?><?php endif; ?>

						· <?php echo news_h(news_front_format_date($published)); ?>

						<?php if ($updated !== '' && $updated !== $published): ?>

							· Mis à jour <?php echo news_h(news_front_format_date($updated)); ?>

						<?php endif; ?>

						· <?php echo (int) $reading; ?> min de lecture

					</span>

				</p>

			</div>

			<?php if ($sources): news_component_trust_badge($sources); endif; ?>

		</header>



		<?php if ($heroImg !== ''):
			$alt = trim((string) ($article['alt_text'] ?? ''));
			if ($alt === '' || $alt === (string) $article['h1']) {
				$alt = (string) ($article['short_headline'] ?: $article['h1']);
			}
			?>
			<figure class="mn-article__hero mn-article__hero--lead">
				<picture>
					<?php if ($heroPortrait !== '' && $heroPortrait !== $heroLandscape): ?>
						<source media="(max-width: 767px)" srcset="<?php echo news_h($heroPortrait); ?>" width="720" height="960">
					<?php endif; ?>
					<img
						src="<?php echo news_h($heroLandscape); ?>"
						alt="<?php echo news_h($alt); ?>"
						width="960"
						height="540"
						sizes="(max-width: 767px) 100vw, min(820px, calc(100vw - 40px))"
						fetchpriority="high"
						decoding="async"
					>
				</picture>
			</figure>
		<?php endif; ?>



		<div class="mn-article__content mn-article__content--narrow">

			<?php echo (string) $article['content_html']; ?>

		</div>



		<div class="mn-article__footer-blocks mn-article__footer-blocks--narrow">

			<?php
			try {
				if (trim((string) ($article['airline_block_json'] ?? '')) !== '') {
					require_once __DIR__ . '/includes/airline/AirlineBlockRenderer.php';
					$airlineBlock = NewsAirlineBlockRenderer::renderForArticle($article);
					if ($airlineBlock !== '') {
						echo $airlineBlock;
					}
				}
			} catch (Throwable $e) {
				if (function_exists('news_log_error')) {
					news_log_error('airline_block: ' . $e->getMessage());
				}
			}
			?>

			<?php

			try {

				require_once __DIR__ . '/includes/commerce/CommerceBlockRenderer.php';

				$commerceEnd = NewsCommerceBlockRenderer::renderForArticle($article, 'end_of_article');

				if ($commerceEnd !== '') {

					echo $commerceEnd;

				}

			} catch (Throwable $e) {

				if (function_exists('news_log_error')) {

					news_log_error('article_commerce: ' . $e->getMessage());

				}

			}

			?>



			<section class="mn-author-box mn-author-box--card" aria-label="Auteur">

				<div class="mn-author-box__avatar mn-author-box__avatar--harcourt">

					<?php news_front_persona_avatar($article, 'mn-team-portrait', 88, $author); ?>

				</div>

				<div class="mn-author-box__body">

					<h2 class="mn-author-box__name"><?php echo news_h($author); ?></h2>

					<p class="mn-author-box__role"><?php echo news_h((string) ($article['persona_role'] ?? 'Rédaction Magellio News')); ?></p>

					<?php if (!empty($article['persona_bio'])): ?>

						<p class="mn-author-box__bio"><?php echo news_h((string) $article['persona_bio']); ?></p>

					<?php endif; ?>

					<?php if ($authorSlug !== ''): ?>

						<a class="mn-author-box__link" href="<?php echo news_h(news_url('equipe/' . $authorSlug . '.html')); ?>">Voir le profil de <?php echo news_h($author); ?></a>

					<?php endif; ?>

				</div>

			</section>



			<?php if ($related): ?>

				<section class="mn-related" aria-labelledby="related-title">

					<h2 id="related-title" class="mn-section__title">À lire aussi</h2>

					<div class="mn-related__list">

						<?php foreach ($related as $row): ?>

							<?php news_front_card($row, 'horizontal', ['meta_avatar' => false]); ?>

						<?php endforeach; ?>

					</div>

				</section>

			<?php endif; ?>



			<section class="mn-follow mn-follow--inline">

				<h2 class="mn-section__title">Suivre Magellio News</h2>

				<p>Recevez les publications dans votre lecteur RSS.</p>

				<p class="mn-follow__actions">

					<a class="mn-button" href="<?php echo news_h(news_url('rss.xml')); ?>">Flux RSS principal</a>

					<a class="mn-button mn-button--ghost" href="<?php echo news_h(news_url('rss/')); ?>">Tous les flux</a>

				</p>

			</section>

		</div>

	</article>

</div>



<?php news_front_layout_end(); ?>

