<?php
/**
 * Template part for displaying post cards in Modern Card Grid layout
 *
 * @package Blog_Over
 */

$image_type = get_theme_mod('blog_over_post_image_type', 'fixed');
$elements = blog_over_get_blog_entry_elements();
$show_thumbnail = get_theme_mod('Blog_Over_show_thumbnail', true);
$ratio_class = function_exists('blog_over_get_image_ratio_class') ? blog_over_get_image_ratio_class() : 'ratio-full';
$thumbnail_class = ($ratio_class !== 'ratio-full') ? $ratio_class : ($image_type === 'fixed' ? 'fixed-height' : 'auto-height');
?>

<article id="post-<?php the_ID(); ?>" <?php post_class('blog-over-modern-card'); ?>>
    <div class="blog-over-modern-card-inner">
        <?php
        // Featured Image - fixed at top, controlled by toggle
        if ($show_thumbnail && has_post_thumbnail()):
            ?>
            <div class="blog-over-modern-thumbnail <?php echo esc_attr($thumbnail_class); ?>">
                <a href="<?php the_permalink(); ?>">
                    <?php the_post_thumbnail('blog-over-featured'); ?>
                </a>
            </div>
            <?php
        endif;
        ?>

        <div class="blog-over-modern-content">
            <?php
            // Render elements in saved order
            foreach ($elements as $element):
                switch ($element):
                    case 'category':
                        // Category only shows when thumbnail is enabled
                        if ($show_thumbnail):
                            $categories = get_the_category();
                            if (!empty($categories)):
                                ?>
                                <div class="blog-over-modern-category">
                                    <?php foreach ($categories 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;
                        break;

                    case 'header':
                        ?>
                        <header class="blog-over-modern-header">
                            <?php the_title('<h2 class="blog-over-modern-title"><a href="' . esc_url(get_permalink()) . '">', '</a></h2>'); ?>
                        </header>
                        <?php
                        break;

                    case 'meta':
                        ?>
                        <div class="blog-over-modern-meta">
                            <?php blog_over_render_entry_meta(); ?>
                            <?php edit_post_link('<i class="fas fa-edit"></i>', '<span class="blog-over-modern-edit">', '</span>'); ?>
                        </div>
                        <?php
                        break;

                    case 'summary':
                        ?>
                        <div class="blog-over-modern-excerpt">
                            <?php
                            if (get_theme_mod('blog_over_content_type', 'excerpt') === 'excerpt') {
                                the_excerpt();
                            } else {
                                the_content();
                            }
                            ?>
                        </div>
                        <?php
                        break;

                    case 'footer':
                        ?>
                        <footer class="blog-over-modern-footer">
                            <a href="<?php the_permalink(); ?>" class="blog-over-modern-readmore">
                                <?php echo esc_html(get_theme_mod('blog_over_read_more_text', __('Read More', 'blog-over'))); ?>
                                <i class="fas fa-arrow-right"></i>
                            </a>
                        </footer>
                        <?php
                        break;
                endswitch;
            endforeach;
            ?>
        </div>
    </div>
</article>