<?php
/**
 * Template part for displaying posts in the Editorial List layout.
 *
 * Layout modes (set by Pro plugin via blog_over_editorial_alignment):
 *   alternating  — Z-Pattern (odd: img-left, even: img-right)
 *   left-image   — Image always on left
 *   right-image  — Image always on right
 *
 * @package Blog_Over
 */

// Determine post index (set by loop in index.php)
$blog_over_editorial_index = (int) get_query_var('blog_over_editorial_index', 0);

// Shared settings
$elements     = blog_over_get_blog_entry_elements();
$show_thumb   = get_theme_mod('Blog_Over_show_thumbnail', true);
$cats         = get_the_category();

// ─── Alignment ────────────────────────────────────────────
$alignment = get_theme_mod('blog_over_editorial_alignment', 'alternating');

// Z-Pattern: ALL posts are full-width featured cards (image alternates per index)
// Left Image: ALL posts image on left
// Right Image: ALL posts image on right
$is_featured = true;

// ─── Image side (only relevant for featured/big cards) ────
if ($alignment === 'alternating') {
    // TRUE Z-Pattern: even index = image LEFT, odd index = image RIGHT
    $img_right = ($blog_over_editorial_index % 2 !== 0);
} elseif ($alignment === 'right-image') {
    $img_right = true;
} else {
    // left-image (default)
    $img_right = false;
}

// ─── Image ratio class (Pro filter hook) ─────────────────
$ratio_class = apply_filters('blog_over_image_ratio_class', 'ratio-full');

// =========================================================
// FEATURED POST — large side-image newspaper card
// =========================================================
if ($is_featured) : ?>

    <?php $no_img_class = (!$show_thumb || !has_post_thumbnail()) ? ' no-image' : ''; ?>
    <article id="post-<?php the_ID(); ?>" <?php post_class('blog-over-editorial-featured' . ($img_right ? ' image-right' : ' image-left') . $no_img_class); ?>>

        <?php if ($show_thumb && has_post_thumbnail()) : ?>
            <div class="blog-over-editorial-featured-image <?php echo esc_attr($ratio_class); ?>">
                <a href="<?php the_permalink(); ?>" aria-label="<?php the_title_attribute(); ?>">
                    <?php the_post_thumbnail('large'); ?>
                </a>

                <?php if (!empty($cats) && in_array('category', $elements)) : ?>
                    <div class="blog-over-editorial-featured-cat">
                        <?php foreach ($cats as $cat) : ?>
                            <a href="<?php echo esc_url(get_category_link($cat->term_id)); ?>">
                                <?php echo esc_html($cat->name); ?>
                            </a>
                        <?php endforeach; ?>
                    </div>
                <?php endif; ?>
            </div>
        <?php endif; ?>

        <div class="blog-over-editorial-featured-content">

            <?php // Category badge — only when NO image (inline position)
            if (!$show_thumb || !has_post_thumbnail()) :
                if (!empty($cats) && in_array('category', $elements)) : ?>
                    <div class="blog-over-editorial-featured-cat is-inline">
                        <?php foreach ($cats as $cat) : ?>
                            <a href="<?php echo esc_url(get_category_link($cat->term_id)); ?>">
                                <?php echo esc_html($cat->name); ?>
                            </a>
                        <?php endforeach; ?>
                    </div>
                <?php endif;
            endif; ?>

            <?php // Meta (author / date)
            if (in_array('meta', $elements)) : ?>
                <div class="blog-over-editorial-featured-meta">
                    <?php blog_over_render_entry_meta(); ?>
                </div>
            <?php endif; ?>

            <?php // Title
            if (in_array('header', $elements)) : ?>
                <h2 class="blog-over-editorial-featured-title">
                    <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
                </h2>
            <?php endif; ?>

            <?php // Excerpt / content
            if (in_array('summary', $elements)) : ?>
                <div class="blog-over-editorial-featured-excerpt">
                    <?php
                    if (get_theme_mod('blog_over_content_type', 'excerpt') === 'excerpt') {
                        the_excerpt();
                    } else {
                        the_content();
                    }
                    ?>
                </div>
            <?php endif; ?>

            <?php // Read-more CTA
            if (in_array('footer', $elements)) : ?>
                <a href="<?php the_permalink(); ?>" class="blog-over-editorial-readmore">
                    <?php echo esc_html(get_theme_mod('blog_over_read_more_text', __('Read More', 'blog-over'))); ?>
                    <i class="fas fa-arrow-right" aria-hidden="true"></i>
                </a>
            <?php endif; ?>

        </div>
    </article>

<?php
// =========================================================
// LIST ITEMS — compact horizontal rows
// =========================================================
else :
    $item_number = $blog_over_editorial_index;
?>

    <article id="post-<?php the_ID(); ?>" <?php post_class('blog-over-editorial-item'); ?>>

        <span class="blog-over-editorial-item-number" aria-hidden="true">
            <?php echo esc_html($item_number); ?>
        </span>

        <?php if ($show_thumb && has_post_thumbnail()) : ?>
            <div class="blog-over-editorial-item-thumb">
                <a href="<?php the_permalink(); ?>">
                    <?php the_post_thumbnail('thumbnail'); ?>
                </a>
            </div>
        <?php endif; ?>

        <div class="blog-over-editorial-item-content">

            <?php if (!empty($cats) && in_array('category', $elements)) : ?>
                <div class="blog-over-editorial-item-cat">
                    <a href="<?php echo esc_url(get_category_link($cats[0]->term_id)); ?>">
                        <?php echo esc_html($cats[0]->name); ?>
                    </a>
                </div>
            <?php endif; ?>

            <?php if (in_array('header', $elements)) : ?>
                <h3 class="blog-over-editorial-item-title">
                    <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
                </h3>
            <?php endif; ?>

            <?php if (in_array('meta', $elements)) : ?>
                <div class="blog-over-editorial-item-meta">
                    <?php blog_over_render_entry_meta(); ?>
                </div>
            <?php endif; ?>

        </div>
    </article>

<?php endif; ?>
