<?php
/**
 * Listado de noticias en home con paginación (5 por página).
 */
if (!defined('access') || !access) {
	die();
}

$News = new News();
$newsList = loadCache('news.cache');
if (!is_array($newsList)) {
	return;
}

$articles = array();
foreach ($newsList as $key => $newsArticle) {
	if (!is_array($newsArticle) || empty($newsArticle['news_id'])) {
		continue;
	}
	$articles[] = $newsArticle;
}

$total = count($articles);
if ($total === 0) {
	return;
}

$perPage = 5;
$nexNewsPage = 1;
if (isset($_GET['nex_np']) && is_numeric($_GET['nex_np'])) {
	$nexNewsPage = max(1, (int) $_GET['nex_np']);
}

$totalPages = (int) max(1, ceil($total / $perPage));
if ($nexNewsPage > $totalPages) {
	$nexNewsPage = $totalPages;
}

$offset = ($nexNewsPage - 1) * $perPage;
$pageArticles = array_slice($articles, $offset, $perPage);

$nexNewsPageUrl = function ($pageNum) {
	$params = isset($_GET) && is_array($_GET) ? $_GET : array();
	$params['nex_np'] = (int) $pageNum;
	return rtrim(__BASE_URL__, '/') . '/?' . http_build_query($params);
};

foreach ($pageArticles as $newsArticle) {
	$newsMeta = function_exists('nexoraNewsStudioGet') ? nexoraNewsStudioGet($newsArticle['news_id']) : array();
	$News->setId($newsArticle['news_id']);
	$news_title = base64_decode($newsArticle['news_title']);
	if ($news_title === false) {
		$news_title = (string) $newsArticle['news_title'];
	}

	if (config('language_switch_active', true)) {
		if (isset($_SESSION['language_display']) && isset($newsArticle['translations']) && is_array($newsArticle['translations']) && array_key_exists($_SESSION['language_display'], $newsArticle['translations'])) {
			$tr = base64_decode($newsArticle['translations'][$_SESSION['language_display']]);
			$news_title = $tr !== false ? $tr : (string) $newsArticle['translations'][$_SESSION['language_display']];
		}
	}

	$news_url = __BASE_URL__ . 'news/' . $newsArticle['news_id'] . '/';

	echo '<div class="nex-news-row">';
	$typeLabels = array('anuncio'=>'Anuncio','actualizacion'=>'Actualización','evento'=>'Evento','mantenimiento'=>'Mantenimiento','comunidad'=>'Comunidad');
	$typeKey = isset($newsMeta['type']) ? $newsMeta['type'] : 'anuncio';
	echo '<span class="nex-news-type nex-news-type--' . htmlspecialchars($typeKey, ENT_QUOTES, 'UTF-8') . '">' . htmlspecialchars($typeLabels[$typeKey] ?? 'Noticia', ENT_QUOTES, 'UTF-8') . '</span>';
	echo '<span class="nex-news-title"><a href="' . htmlspecialchars($news_url, ENT_QUOTES, 'UTF-8') . '">' . htmlspecialchars($news_title, ENT_QUOTES, 'UTF-8') . '</a></span>';
	echo '<span class="nex-news-date">' . htmlspecialchars(date('Y/m/d', $newsArticle['news_date']), ENT_QUOTES, 'UTF-8') . '</span>';
	echo '</div>';
}

if ($totalPages > 1) {
	$prev = max(1, $nexNewsPage - 1);
	$next = min($totalPages, $nexNewsPage + 1);
	$labelPrev = htmlspecialchars(nexora_lang('nexora_news_prev', 'Anterior'), ENT_QUOTES, 'UTF-8');
	$labelNext = htmlspecialchars(nexora_lang('nexora_news_next', 'Siguiente'), ENT_QUOTES, 'UTF-8');

	echo '<nav class="nex-news-pagination" aria-label="' . htmlspecialchars(nexora_lang('nexora_news_pagination_aria', 'Paginación de noticias'), ENT_QUOTES, 'UTF-8') . '">';
	echo '<div class="nex-news-pagination-inner">';

	if ($nexNewsPage > 1) {
		echo '<a class="nex-news-page-btn" href="' . htmlspecialchars($nexNewsPageUrl($prev), ENT_QUOTES, 'UTF-8') . '">' . $labelPrev . '</a>';
	} else {
		echo '<span class="nex-news-page-btn nex-news-page-btn--disabled" aria-disabled="true">' . $labelPrev . '</span>';
	}

	echo '<span class="nex-news-page-info">';
	echo htmlspecialchars(nexora_lang('nexora_news_page_of', 'Página'), ENT_QUOTES, 'UTF-8');
	echo ' <strong>' . (int) $nexNewsPage . '</strong> / ' . (int) $totalPages;
	echo '</span>';

	if ($nexNewsPage < $totalPages) {
		echo '<a class="nex-news-page-btn" href="' . htmlspecialchars($nexNewsPageUrl($next), ENT_QUOTES, 'UTF-8') . '">' . $labelNext . '</a>';
	} else {
		echo '<span class="nex-news-page-btn nex-news-page-btn--disabled" aria-disabled="true">' . $labelNext . '</span>';
	}

	echo '</div>';
	echo '</nav>';
}
