<?php
/**
 * Template part for displaying the You Missed Section.
 *
 * @package Blog_Over
 */

// Check if section is enabled
$enable_missed = get_theme_mod('blog_over_you_missed_show', false);
if (!$enable_missed) {
    return;
}

// Check page visibility
$show_on = get_theme_mod('blog_over_you_missed_show_on', array('blog', 'single', 'archive'));
if (empty($show_on)) {
    return;
}

// Check current page type
$should_show = false;
if (is_front_page() && is_home()) {
    $should_show = in_array('blog', $show_on) || in_array('home', $show_on);
} elseif (is_front_page()) {
    $should_show = in_array('home', $show_on);
} elseif (is_home()) {
    $should_show = in_array('blog', $show_on);
} elseif (is_single()) {
    $should_show = in_array('single', $show_on);
} elseif (is_page()) {
    $should_show = in_array('page', $show_on);
} elseif (is_search()) {
    $should_show = in_array('search', $show_on);
} elseif (is_archive()) {
    $should_show = in_array('archive', $show_on);
}

if (!$should_show) {
    return;
}

// Get settings
$missed_title = get_theme_mod('blog_over_you_missed_title', __('You Missed', 'blog-over'));
$count = get_theme_mod('blog_over_you_missed_count', 4);
$layout = get_theme_mod('blog_over_you_missed_layout', 'default');
$columns = get_theme_mod('blog_over_you_missed_columns', '4');
$category = get_theme_mod('blog_over_you_missed_category', 'all');

// Sortable elements - card elements
$card_elements = get_theme_mod('blog_over_you_missed_elements', array('category', 'title', 'meta'));
if (!is_array($card_elements)) {
    $card_elements = array('category', 'title', 'meta');
}

// Sortable meta elements (like blog settings)
$meta_elements = get_theme_mod('blog_over_you_missed_meta_elements', array('date'));
if (!is_array($meta_elements)) {
    $meta_elements = array('date');
}

// Build query args
$args = array(
    'post_type' => 'post',
    'posts_per_page' => intval($count),
    'ignore_sticky_posts' => true,
);

// Filter by category if set
if ($category !== 'all' && !empty($category)) {
    if (is_numeric($category)) {
        // Backward compatibility: old installs may still store a numeric ID.
        $args['cat'] = intval($category);
    } else {
        // Slug-based query (preferred).
        $args['category_name'] = sanitize_title($category);
    }
}

$missed_query = new WP_Query($args);

if (!$missed_query->have_posts()) {
    return;
}

// Layout and column classes
$layout_class = 'blog-over-missed-' . esc_attr($layout);
$cols_class = 'blog-over-missed-cols-' . esc_attr($columns);

// Section Width
$section_width = get_theme_mod('blog_over_you_missed_section_width', 'boxed');
$container_class = ($section_width === 'full-width') ? 'blog-over-container blog-over-container-wide' : 'blog-over-container';
?>
<div class="blog-over-missed-wrapper <?php echo esc_attr($layout_class); ?>">
    <div class="<?php echo esc_attr($container_class); ?>">
        <?php if ($missed_title): ?>
            <div class="blog-over-section-heading-wrap">
                <h2 class="blog-over-section-heading blog-over-missed-heading"><?php echo esc_html($missed_title); ?></h2>
            </div>
        <?php endif; ?>

        <div class="blog-over-missed-grid <?php echo esc_attr($cols_class); ?>">
            <?php while ($missed_query->have_posts()):
                $missed_query->the_post();
                $thumb_url = get_the_post_thumbnail_url(get_the_ID(), 'blog-over-medium');
                ?>
                <div class="blog-over-missed-item">
                    <div class="blog-over-missed-thumb" <?php if ($thumb_url): ?>style="background-image: url('<?php echo esc_url($thumb_url); ?>');" role="img" aria-label="<?php echo esc_attr(get_the_title()); ?>"<?php endif; ?>>
                        <a href="<?php the_permalink(); ?>" class="blog-over-missed-link-overlay"
                            aria-label="<?php echo esc_attr(get_the_title()); ?>"></a>
                    </div>
                    <div class="blog-over-missed-content">
                        <?php
                        // Render card elements in sortable order
                        foreach ($card_elements as $element):
                            switch ($element):
                                case 'category':
                                    // Category for all layouts - show up to 2 categories
                                    $cats = get_the_category();
                                    if (!empty($cats)):
                                        $max_cats = min(2, count($cats)); // Show max 2 categories
                                        ?>
                                        <div class="blog-over-missed-category">
                                            <?php for ($i = 0; $i < $max_cats; $i++): ?>
                                                <a href="<?php echo esc_url(get_category_link($cats[$i]->term_id)); ?>">
                                                    <?php echo esc_html($cats[$i]->name); ?>
                                                </a>
                                            <?php endfor; ?>
                                        </div>
                                        <?php
                                    endif;
                                    break;

                                case 'title':
                                    ?>
                                    <h3 class="blog-over-missed-title">
                                        <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
                                    </h3>
                                    <?php
                                    break;

                                case 'meta':
                                    if (!empty($meta_elements)):
                                        ?>
                                        <div class="blog-over-missed-meta">
                                            <?php foreach ($meta_elements as $meta_item): ?>
                                                <?php if ($meta_item === 'author'): ?>
                                                    <span class="blog-over-author">
                                                        <a href="<?php echo esc_url(get_author_posts_url(get_the_author_meta('ID'))); ?>">
                                                            <i class="far fa-user"></i>
                                                            <?php the_author(); ?>
                                                        </a>
                                                    </span>
                                                <?php elseif ($meta_item === 'date'): ?>
                                                    <span class="blog-over-date">
                                                        <a href="<?php the_permalink(); ?>">
                                                            <i class="far fa-calendar-alt"></i>
                                                            <?php echo get_the_date(); ?>
                                                        </a>
                                                    </span>
                                                <?php elseif ($meta_item === 'comments' && get_comments_number() > 0): ?>
                                                    <span class="blog-over-comments">
                                                        <i class="far fa-comment"></i>
                                                        <?php echo get_comments_number(); ?>
                                                    </span>
                                                <?php endif; ?>
                                            <?php endforeach; ?>
                                        </div>
                                        <?php
                                    endif;
                                    break;
                            endswitch;
                        endforeach;
                        ?>
                    </div>
                </div>
            <?php endwhile; ?>
        </div>
    </div>
</div>
<?php
wp_reset_postdata();
?>