<?php
/**
 * Template part – Category Showcase Section
 *
 * Displays categories as rich visual cards.
 * Card image = featured image of the most recent post in that category.
 * Layout: Equal Grid (free) | Masonry / List / Feature Grid (Pro)
 *
 * @package Blog_Over
 * @since   1.1.0
 */

// ── Guard: enabled? ──────────────────────────────────────────────────────────
if ( ! get_theme_mod( 'blog_over_catshowcase_enable', false ) ) {
    return;
}

// ── Guard: page visibility ───────────────────────────────────────────────────
$show_on    = get_theme_mod( 'blog_over_catshowcase_show_on', array( 'home' ) );
$show_on    = is_array( $show_on ) ? $show_on : array();
$should_show = false;

if ( in_array( 'home',    $show_on ) && is_front_page() )                                              $should_show = true;
if ( in_array( 'blog',    $show_on ) && is_home() && ! is_front_page() )                               $should_show = true;
if ( in_array( 'archive', $show_on ) && ( is_archive() || is_category() || is_tag() || is_date() ) )   $should_show = true;

if ( ! $should_show ) {
    return;
}

// ── Gather settings ──────────────────────────────────────────────────────────
$section_title   = get_theme_mod( 'blog_over_catshowcase_title',           __( 'Explore Topics', 'blog-over' ) );
$cat_count       = absint( get_theme_mod( 'blog_over_catshowcase_count',   6 ) );
$show_count      = (bool) get_theme_mod( 'blog_over_catshowcase_show_count',       true );
$show_desc       = (bool) get_theme_mod( 'blog_over_catshowcase_show_description', false );
$layout          = get_theme_mod( 'blog_over_catshowcase_layout',          'grid' );
$card_style      = get_theme_mod( 'blog_over_catshowcase_card_style',      'overlay' );
$columns         = sanitize_key( get_theme_mod( 'blog_over_catshowcase_columns', '3' ) );
$overlay_opacity = absint( get_theme_mod( 'blog_over_catshowcase_overlay_opacity', 55 ) );
$hover_zoom      = (bool) get_theme_mod( 'blog_over_catshowcase_hover_zoom', true );
$section_width   = get_theme_mod( 'blog_over_catshowcase_section_width',   'boxed' );

// ── Query categories ─────────────────────────────────────────────────────────
$categories = get_categories( array(
    'orderby'    => 'count',
    'order'      => 'DESC',
    'hide_empty' => true,
    'number'     => $cat_count,
) );

// Allow Pro to override / filter categories list
$categories = apply_filters( 'blog_over_catshowcase_categories', $categories );

if ( empty( $categories ) || is_wp_error( $categories ) ) {
    return;
}

// ── Container class ──────────────────────────────────────────────────────────
$container_class = ( $section_width === 'full-width' )
    ? 'blog-over-container blog-over-container-wide'
    : 'blog-over-container';

$grid_class   = 'blog-over-catshowcase-cols-' . esc_attr( $columns );
$layout_class = 'blog-over-catshowcase-layout-' . esc_attr( $layout );
$style_class  = 'blog-over-catshowcase-style-' . esc_attr( $card_style );
$zoom_class   = $hover_zoom ? ' blog-over-catshowcase-zoom' : '';

// Overlay opacity as decimal (0-1)
$overlay_alpha = round( $overlay_opacity / 100, 2 );
?>

<div class="blog-over-catshowcase-wrapper <?php echo esc_attr( $layout_class ); ?>">
    <div class="<?php echo esc_attr( $container_class ); ?>">

        <?php if ( $section_title ) : ?>
            <div class="blog-over-section-heading-wrap">
                <h2 class="blog-over-section-heading blog-over-catshowcase-heading">
                    <?php echo esc_html( $section_title ); ?>
                </h2>
            </div>
        <?php endif; ?>

        <div class="blog-over-catshowcase-grid <?php echo esc_attr( $grid_class . ' ' . $style_class . $zoom_class ); ?>">

            <?php foreach ( $categories as $category ) :

                // Fetch the latest post thumbnail for this category as background
                $thumb_url = '';
                $cat_posts = get_posts( array(
                    'numberposts'      => 1,
                    'category'         => $category->term_id,
                    'suppress_filters' => true,
                ) );
                if ( ! empty( $cat_posts ) && has_post_thumbnail( $cat_posts[0]->ID ) ) {
                    $thumb_url = get_the_post_thumbnail_url( $cat_posts[0]->ID, 'blog-over-medium' );
                }

                $cat_link        = get_category_link( $category->term_id );
                $cat_name        = $category->name;
                $cat_description = $category->description;
                $cat_count_posts = $category->count;
            ?>

            <a href="<?php echo esc_url( $cat_link ); ?>"
               class="blog-over-catshowcase-card"
               aria-label="<?php echo esc_attr( sprintf( __( 'Explore %s', 'blog-over' ), $cat_name ) ); ?>">

                <!-- Background Image -->
                <?php if ( $thumb_url ) : ?>
                    <div class="blog-over-catshowcase-bg"
                         style="background-image: url('<?php echo esc_url( $thumb_url ); ?>');"
                         role="img"
                         aria-hidden="true"></div>
                <?php else : ?>
                    <div class="blog-over-catshowcase-bg blog-over-catshowcase-no-image" aria-hidden="true"></div>
                <?php endif; ?>

                <!-- Overlay -->
                <div class="blog-over-catshowcase-overlay"
                     style="background: linear-gradient(to top, rgba(0,0,0,<?php echo esc_attr( $overlay_alpha ); ?>) 0%, rgba(0,0,0,<?php echo esc_attr( round( $overlay_alpha * 0.35, 2 ) ); ?>) 60%, transparent 100%);"
                     aria-hidden="true"></div>

                <!-- Card Content -->
                <div class="blog-over-catshowcase-content">

                    <?php if ( $show_count ) : ?>
                        <span class="blog-over-catshowcase-count">
                            <?php
                            printf(
                                /* translators: %d: post count */
                                esc_html( _n( '%d Post', '%d Posts', $cat_count_posts, 'blog-over' ) ),
                                number_format_i18n( $cat_count_posts )
                            );
                            ?>
                        </span>
                    <?php endif; ?>

                    <h3 class="blog-over-catshowcase-name"><?php echo esc_html( $cat_name ); ?></h3>

                    <?php if ( $show_desc && $cat_description ) : ?>
                        <p class="blog-over-catshowcase-desc">
                            <?php echo esc_html( wp_trim_words( $cat_description, 12, '…' ) ); ?>
                        </p>
                    <?php endif; ?>

                    <span class="blog-over-catshowcase-arrow" aria-hidden="true">
                        <i class="fas fa-arrow-right"></i>
                    </span>

                </div><!-- .blog-over-catshowcase-content -->

            </a><!-- .blog-over-catshowcase-card -->

            <?php endforeach; ?>

        </div><!-- .blog-over-catshowcase-grid -->

    </div><!-- .container -->
</div><!-- .blog-over-catshowcase-wrapper -->
