<?php
$current_page = 'blog';
include 'config.php';

$slug = isset($_GET['slug']) ? $_GET['slug'] : '';

if (!empty($slug)) {
    $posts = wp_api_get('/posts', ['slug' => $slug, '_embed' => '']);

    if (isset($posts['_error'])) {
        $page_title = 'Error | Hugo Supo';
        $page_description = 'Error al cargar el artículo.';
        $page_canonical = 'https://hugosupo.com/blog/';
        $page_og_image = 'https://hugosupo.com/img/hero-slide-1.webp';
        include 'header.php';
        ?>
        <header class="page-header">
            <div class="container text-center">
                <h1>Error al cargar el artículo</h1>
                <p class="lead text-danger"><?php echo escape_html($posts['_error']); ?></p>
                <a href="blog.php" class="btn btn-custom-red mt-4">Ver todas las noticias</a>
            </div>
        </header>
        <?php
        include 'footer.php';
        exit;
    }

    if (empty($posts)) {
        http_response_code(404);
        $page_title = 'Artículo no encontrado | Hugo Supo';
        $page_description = 'El artículo que buscas no existe.';
        $page_canonical = 'https://hugosupo.com/blog/';
        $page_og_image = 'https://hugosupo.com/img/hero-slide-1.webp';
        include 'header.php';
        ?>
        <header class="page-header">
            <div class="container text-center">
                <h1>Artículo no encontrado</h1>
                <p class="lead">El artículo que buscas no existe o ha sido eliminado.</p>
                <a href="blog.php" class="btn btn-custom-red mt-4">Ver todas las noticias</a>
            </div>
        </header>
        <?php
        include 'footer.php';
        exit;
    }

    $post = $posts[0];
    $title = $post['title']['rendered'] ?? 'Sin título';
    $content = $post['content']['rendered'] ?? '';
    $date = format_date($post['date']);
    $category = get_post_category($post);
    $featured_img = get_featured_image($post);
    $featured_alt = get_featured_image_alt($post);
    $excerpt = get_post_excerpt($post, 25);

    $page_title = html_entity_decode(strip_tags($title), ENT_QUOTES, 'UTF-8') . ' | Hugo Supo';
    $page_description = $excerpt;
    $page_canonical = get_post_url($post);
    $page_og_image = $featured_img ?: 'https://hugosupo.com/img/hero-slide-1.webp';
    include 'header.php';
    ?>
        <header class="page-header">
            <div class="container">
                <span class="section-tagline text-white-50"><?php echo escape_html($category); ?></span>
                <h1><?php echo $title; ?></h1>
                <time datetime="<?php echo escape_html($post['date']); ?>" class="text-white-50 d-block mt-2"><i aria-hidden="true" class="fa-regular fa-calendar me-2"></i><?php echo $date; ?></time>
            </div>
        </header>

        <section class="article-content-section">
            <div class="container">
                <div class="row justify-content-center">
                    <div class="col-lg-10 col-xl-8">
                        <?php if (has_featured_image($post)): ?>
                        <figure class="mb-5">
                            <img src="<?php echo escape_html($featured_img); ?>" alt="<?php echo escape_html($featured_alt); ?>" class="img-fluid w-100" style="max-height: 500px; object-fit: cover;">
                        </figure>
                        <?php endif; ?>

                        <div class="article-prose">
                            <?php echo $content; ?>
                        </div>

                        <div class="text-center mt-5 pt-4 border-top">
                            <a href="blog.php" class="btn btn-custom-red"><i aria-hidden="true" class="fa-solid fa-arrow-left me-2"></i>Más noticias</a>
                        </div>
                    </div>
                </div>
            </div>
        </section>
    <?php
    include 'footer.php';
    exit;
}

$page_title = 'Noticias y Actividades | Hugo Supo';
$page_description = 'Noticias, actividades y día a día de Hugo Supo en Puno.';
$page_canonical = 'https://hugosupo.com/blog/';
$page_og_image = 'https://hugosupo.com/img/photo_2026-07-17_09-29-07.jpg';
include 'header.php';

$posts = wp_api_get('/posts', ['_embed' => '', 'per_page' => WP_POSTS_PER_PAGE]);
?>
        <header class="page-header blog-header">
            <div class="container">
                <span class="section-tagline text-white-50">HUGO EN ACCIÓN</span>
                <h1>Noticias y Actividades</h1>
                <p>Conoce el día a día, los recorridos y las propuestas de nuestra campaña.</p>
            </div>
        </header>

<?php if (empty($posts)): ?>
        <section class="news-grid-section">
            <div class="container text-center py-5">
                <i aria-hidden="true" class="fa-solid fa-cloud-arrow-down fs-1 text-muted mb-4 d-block"></i>
                <h2 class="section-title section-title-center">No pudimos cargar las noticias</h2>
                <p class="lead text-muted mx-auto page-intro">Estamos teniendo problemas de conexión con el servidor. Por favor, intenta nuevamente más tarde.</p>
            </div>
        </section>
<?php elseif (isset($posts['_error'])): ?>
        <section class="news-grid-section">
            <div class="container text-center py-5">
                <i aria-hidden="true" class="fa-solid fa-triangle-exclamation fs-1 text-danger mb-4 d-block"></i>
                <h2 class="section-title section-title-center">Error al obtener noticias</h2>
                <p class="lead text-danger mx-auto page-intro"><?php echo escape_html($posts['_error']); ?></p>
                <p class="text-muted">Revisa el archivo de errores del servidor para más detalles o contacta al administrador del CMS.</p>
            </div>
        </section>
<?php else:
    $first = $posts[0];
    $first_img = get_featured_image($first);
    $first_alt = get_featured_image_alt($first);
    $first_cat = get_post_category($first);
    $first_date = format_date($first['date']);
    $first_title = $first['title']['rendered'] ?? '';
    $first_excerpt = get_post_excerpt($first, 30);
?>
        <section class="featured-news-section">
            <div class="container">
                <article class="featured-news">
                    <div class="row g-0 align-items-stretch">
                        <div class="col-lg-7">
                            <?php if (has_featured_image($first)): ?>
                            <img src="<?php echo escape_html($first_img); ?>" alt="<?php echo escape_html($first_alt); ?>">
                            <?php endif; ?>
                        </div>
                        <div class="col-lg-5">
                            <div class="featured-news-content">
                                <span class="news-category"><?php echo escape_html($first_cat); ?></span>
                                <time datetime="<?php echo escape_html($first['date']); ?>"><i aria-hidden="true" class="fa-regular fa-calendar me-2"></i><?php echo $first_date; ?></time>
                                <h2><?php echo $first_title; ?></h2>
                                <p><?php echo $first_excerpt; ?></p>
                                <a href="<?php echo escape_html(get_post_url($first)); ?>" class="btn btn-custom-red btn-sm mt-3 align-self-start">Leer más</a>
                            </div>
                        </div>
                    </div>
                </article>
            </div>
        </section>

        <section class="news-grid-section">
            <div class="container">
                <div class="d-flex flex-wrap justify-content-between align-items-end gap-3 mb-5">
                    <div>
                        <span class="section-tagline">ÚLTIMAS PUBLICACIONES</span>
                        <h2 class="section-title mb-0">El día a día de Hugo</h2>
                    </div>
                    <div class="news-filters" aria-label="Categorías">
                        <button class="active" data-filter="all" type="button">Todas</button>
                        <?php
                        $all_cats = array();
                        foreach ($posts as $p) {
                            $all_cats[] = get_post_category($p);
                        }
                        foreach (array_unique($all_cats) as $cat):
                        ?>
                        <button data-filter="<?php echo escape_html(strtolower($cat)); ?>" type="button"><?php echo escape_html($cat); ?></button>
                        <?php endforeach; ?>
                    </div>
                </div>
                <div class="row g-4">
                    <?php for ($i = 1; $i < count($posts); $i++):
                        $p = $posts[$i];
                        $img = get_featured_image($p);
                        $alt = get_featured_image_alt($p);
                        $cat = get_post_category($p);
                        $date = format_date($p['date']);
                        $title = $p['title']['rendered'] ?? '';
                        $excerpt = get_post_excerpt($p, 20);
                    ?>
                    <div class="col-md-6 col-lg-4">
                        <article class="news-card">
                            <?php if (has_featured_image($p)): ?>
                            <img src="<?php echo escape_html($img); ?>" alt="<?php echo escape_html($alt); ?>" loading="lazy">
                            <?php endif; ?>
                            <div class="news-card-body">
                                <span class="news-category"><?php echo escape_html($cat); ?></span>
                                <time datetime="<?php echo escape_html($p['date']); ?>"><?php echo $date; ?></time>
                                <h3><?php echo $title; ?></h3>
                                <p><?php echo $excerpt; ?></p>
                                <a href="<?php echo escape_html(get_post_url($p)); ?>" class="btn btn-custom-red btn-sm mt-3">Leer más</a>
                            </div>
                        </article>
                    </div>
                    <?php endfor; ?>
                </div>
            </div>
        </section>
    <?php endif; ?>

        <section class="news-cta">
            <div class="container text-center">
                <h2>Sé parte de las próximas actividades</h2>
                <p>Únete al equipo y ayúdanos a llevar nuestras propuestas a todo Puno.</p>
                <a href="index.php#unete" class="btn btn-custom-red">Quiero participar</a>
            </div>
        </section>

<?php
include 'footer.php';
