<?php
/**
 * Trending Posts Section – Customizer Settings
 *
 * A dedicated section for surfacing high-engagement or recent popular posts.
 * Completely separate from "You Missed" — different query strategy, different
 * layouts, and different use-case (real-time trending vs. older content).
 *
 * @package Blog_Over
 * @since   1.1.0
 */

// NOTE: Choice functions (blog_over_get_trending_layout_choices, etc.) are
// defined in inc/pro-filters.php to keep everything in one canonical place.

// ─────────────────────────────────────────────────────────────────────────────
// Register Customizer Section
// ─────────────────────────────────────────────────────────────────────────────

Kirki::add_section( 'blog_over_trending_section', array(
    'title'    => esc_html__( 'Trending Posts Section', 'blog-over' ),
    'panel'    => 'blog_over_frontpage_panel',
    'priority' => 45,
) );

// ═══════════════════════════════════════════
// A) GENERAL SETTINGS HEADING
// ═══════════════════════════════════════════
Kirki::add_field( 'Blog_Over_config', array(
    'type'     => 'custom',
    'settings' => 'blog_over_trending_general_heading',
    'section'  => 'blog_over_trending_section',
    'default'  => '<div style="background:linear-gradient(135deg,#eff6ff,#dbeafe);padding:10px 14px;border-radius:6px;border-left:3px solid #3b82f6;"><span style="font-size:13px;font-weight:600;color:#1e40af;text-transform:uppercase;letter-spacing:.5px;">' . esc_html__( 'General Settings', 'blog-over' ) . '</span></div>',
    'priority' => 1,
) );

// ═══════════════════════════════════════════
// B) Enable Toggle
// ═══════════════════════════════════════════
Kirki::add_field( 'Blog_Over_config', array(
    'type'     => 'toggle',
    'settings' => 'blog_over_trending_enable',
    'label'    => esc_html__( 'Enable Trending Posts Section', 'blog-over' ),
    'description' => esc_html__( 'Show a curated list of your most popular or engaging posts.', 'blog-over' ),
    'section'  => 'blog_over_trending_section',
    'default'  => false,
    'priority' => 5,
    'partial_refresh' => array(
        'blog_over_trending_enable' => array(
            'selector'        => '.blog-over-trending-wrapper',
            'render_callback' => 'blog_over_render_trending_partial',
        ),
    ),
) );

// ═══════════════════════════════════════════
// C) Section Title
// ═══════════════════════════════════════════
Kirki::add_field( 'Blog_Over_config', array(
    'type'     => 'text',
    'settings' => 'blog_over_trending_title',
    'label'    => esc_html__( 'Section Title', 'blog-over' ),
    'description' => esc_html__( 'Heading displayed above the trending posts list.', 'blog-over' ),
    'section'  => 'blog_over_trending_section',
    'default'  => esc_html__( 'Trending Now', 'blog-over' ),
    'priority' => 10,
    'active_callback' => array(
        array( 'setting' => 'blog_over_trending_enable', 'operator' => '==', 'value' => true ),
    ),
    'partial_refresh' => array(
        'blog_over_trending_title_refresh' => array(
            'selector'        => '.blog-over-trending-heading',
            'render_callback' => function() {
                return esc_html( get_theme_mod( 'blog_over_trending_title', __( 'Trending Now', 'blog-over' ) ) );
            },
        ),
    ),
) );

// ═══════════════════════════════════════════
// D) Section Label / Badge Text
// ═══════════════════════════════════════════
Kirki::add_field( 'Blog_Over_config', array(
    'type'     => 'text',
    'settings' => 'blog_over_trending_badge_text',
    'label'    => esc_html__( 'Label Badge Text', 'blog-over' ),
    'description' => esc_html__( 'Small badge shown next to the section title. e.g. "Hot", "Popular", "🔥"', 'blog-over' ),
    'section'  => 'blog_over_trending_section',
    'default'  => '🔥 Hot',
    'priority' => 11,
    'active_callback' => array(
        array( 'setting' => 'blog_over_trending_enable', 'operator' => '==', 'value' => true ),
    ),
) );

// ═══════════════════════════════════════════
// E) Show On Pages (Visibility)
// ═══════════════════════════════════════════
Kirki::add_field( 'Blog_Over_config', array(
    'type'        => 'multicheck',
    'settings'    => 'blog_over_trending_show_on',
    'label'       => esc_html__( 'Show On Pages', 'blog-over' ),
    'description' => esc_html__( 'Select the page types where this section should appear.', 'blog-over' ),
    'section'     => 'blog_over_trending_section',
    'default'     => array( 'home', 'blog' ),
    'priority'    => 12,
    'choices'     => array(
        'home'    => esc_html__( 'Home Page', 'blog-over' ),
        'blog'    => esc_html__( 'Blog / Posts Page', 'blog-over' ),
        'archive' => esc_html__( 'Archive Pages', 'blog-over' ),
        'single'  => esc_html__( 'Single Post', 'blog-over' ),
        'page'    => esc_html__( 'Single Page', 'blog-over' ),
    ),
    'active_callback' => array(
        array( 'setting' => 'blog_over_trending_enable', 'operator' => '==', 'value' => true ),
    ),
) );

// ═══════════════════════════════════════════
// F) Section Width
// ═══════════════════════════════════════════
Kirki::add_field( 'Blog_Over_config', array(
    'type'     => 'radio-buttonset',
    'settings' => 'blog_over_trending_section_width',
    'label'    => esc_html__( 'Section Width', 'blog-over' ),
    'section'  => 'blog_over_trending_section',
    'default'  => 'boxed',
    'priority' => 13,
    'choices'  => array(
        'boxed'      => esc_html__( 'Boxed', 'blog-over' ),
        'full-width' => esc_html__( 'Full Width', 'blog-over' ),
    ),
    'active_callback' => array(
        array( 'setting' => 'blog_over_trending_enable', 'operator' => '==', 'value' => true ),
    ),
) );

// ═══════════════════════════════════════════
// QUERY SETTINGS HEADING
// ═══════════════════════════════════════════
Kirki::add_field( 'Blog_Over_config', array(
    'type'     => 'custom',
    'settings' => 'blog_over_trending_query_heading',
    'section'  => 'blog_over_trending_section',
    'default'  => '<div style="background:linear-gradient(135deg,#eff6ff,#dbeafe);padding:10px 14px;border-radius:6px;border-left:3px solid #3b82f6;margin-top:20px;"><span style="font-size:13px;font-weight:600;color:#1e40af;text-transform:uppercase;letter-spacing:.5px;">' . esc_html__( 'Content & Query', 'blog-over' ) . '</span></div>',
    'priority' => 14,
    'active_callback' => array(
        array( 'setting' => 'blog_over_trending_enable', 'operator' => '==', 'value' => true ),
    ),
) );

// ═══════════════════════════════════════════
// G) Sort Method (What defines "Trending")
// ═══════════════════════════════════════════
Kirki::add_field( 'Blog_Over_config', array(
    'type'     => 'radio-buttonset',
    'settings' => 'blog_over_trending_sort_by',
    'label'    => esc_html__( 'Rank Posts By', 'blog-over' ),
    'description' => esc_html__( 'Choose what makes a post "trending". More options available in Pro.', 'blog-over' ),
    'section'  => 'blog_over_trending_section',
    'default'  => 'comments',
    'priority' => 15,
    'choices'  => blog_over_get_trending_sort_choices(),
    'sanitize_callback' => 'blog_over_sanitize_select',
    'active_callback' => array(
        array( 'setting' => 'blog_over_trending_enable', 'operator' => '==', 'value' => true ),
    ),
) );

// ═══════════════════════════════════════════
// H) Time Period
// ═══════════════════════════════════════════
Kirki::add_field( 'Blog_Over_config', array(
    'type'     => 'radio-buttonset',
    'settings' => 'blog_over_trending_period',
    'label'    => esc_html__( 'Time Period', 'blog-over' ),
    'description' => esc_html__( 'Filter posts published within a specific time range. Upgrade to Pro for weekly/monthly filters.', 'blog-over' ),
    'section'  => 'blog_over_trending_section',
    'default'  => 'all_time',
    'priority' => 16,
    'choices'  => blog_over_get_trending_period_choices(),
    'sanitize_callback' => 'blog_over_sanitize_select',
    'active_callback' => array(
        array( 'setting' => 'blog_over_trending_enable', 'operator' => '==', 'value' => true ),
    ),
) );

// ═══════════════════════════════════════════
// I) Number of Posts
// ═══════════════════════════════════════════
Kirki::add_field( 'Blog_Over_config', array(
    'type'     => 'slider',
    'settings' => 'blog_over_trending_count',
    'label'    => esc_html__( 'Number of Posts to Show', 'blog-over' ),
    'section'  => 'blog_over_trending_section',
    'default'  => 5,
    'priority' => 17,
    'choices'  => array( 'min' => 3, 'max' => 10, 'step' => 1 ),
    'active_callback' => array(
        array( 'setting' => 'blog_over_trending_enable', 'operator' => '==', 'value' => true ),
    ),
) );

// ═══════════════════════════════════════════
// J) Filter by Category
// ═══════════════════════════════════════════
Kirki::add_field( 'Blog_Over_config', array(
    'type'     => 'select',
    'settings' => 'blog_over_trending_category',
    'label'    => esc_html__( 'Filter by Category', 'blog-over' ),
    'description' => esc_html__( 'Show trending posts from a specific category, or leave as "All" to include everything.', 'blog-over' ),
    'section'  => 'blog_over_trending_section',
    'default'  => 'all',
    'priority' => 18,
    'choices'  => blog_over_get_categories_dropdown(),
    'active_callback' => array(
        array( 'setting' => 'blog_over_trending_enable', 'operator' => '==', 'value' => true ),
    ),
) );

// ═══════════════════════════════════════════
// DESIGN SETTINGS HEADING
// ═══════════════════════════════════════════
Kirki::add_field( 'Blog_Over_config', array(
    'type'     => 'custom',
    'settings' => 'blog_over_trending_design_heading',
    'section'  => 'blog_over_trending_section',
    'default'  => '<div style="background:linear-gradient(135deg,#eff6ff,#dbeafe);padding:10px 14px;border-radius:6px;border-left:3px solid #3b82f6;margin-top:20px;"><span style="font-size:13px;font-weight:600;color:#1e40af;text-transform:uppercase;letter-spacing:.5px;">' . esc_html__( 'Design Settings', 'blog-over' ) . '</span></div>',
    'priority' => 19,
    'active_callback' => array(
        array( 'setting' => 'blog_over_trending_enable', 'operator' => '==', 'value' => true ),
    ),
) );

// ═══════════════════════════════════════════
// K) Layout Style
// ═══════════════════════════════════════════
Kirki::add_field( 'Blog_Over_config', array(
    'type'     => 'radio-buttonset',
    'settings' => 'blog_over_trending_layout',
    'label'    => esc_html__( 'Layout Style', 'blog-over' ),
    'description' => esc_html__( 'Numbered List is included free. Card Grid & Minimal available in Pro.', 'blog-over' ),
    'section'  => 'blog_over_trending_section',
    'default'  => 'numbered',
    'priority' => 20,
    'choices'  => blog_over_get_trending_layout_choices(),
    'sanitize_callback' => 'blog_over_sanitize_select',
    'active_callback' => array(
        array( 'setting' => 'blog_over_trending_enable', 'operator' => '==', 'value' => true ),
    ),
) );

// ═══════════════════════════════════════════
// K-2) Grid Columns (only for Card Grid)
// ═══════════════════════════════════════════
Kirki::add_field( 'Blog_Over_config', array(
    'type'     => 'radio-buttonset',
    'settings' => 'blog_over_trending_columns',
    'label'    => esc_html__( 'Grid Columns', 'blog-over' ),
    'description' => esc_html__( 'Number of post cards per row on desktop.', 'blog-over' ),
    'section'  => 'blog_over_trending_section',
    'default'  => '3',
    'priority' => 20.5,
    'choices'  => array(
        '2' => esc_html__( '2', 'blog-over' ),
        '3' => esc_html__( '3', 'blog-over' ),
        '4' => esc_html__( '4', 'blog-over' ),
    ),
    'sanitize_callback' => 'blog_over_sanitize_select',
    'active_callback' => function() {
        if ( ! get_theme_mod( 'blog_over_trending_enable', false ) ) {
            return false;
        }
        $layout = get_theme_mod( 'blog_over_trending_layout', 'numbered' );
        return in_array( $layout, array( 'numbered', 'card-grid', 'minimal' ), true );
    },
) );

// ═══════════════════════════════════════════
// L) Show Rank Numbers
// ═══════════════════════════════════════════
Kirki::add_field( 'Blog_Over_config', array(
    'type'     => 'toggle',
    'settings' => 'blog_over_trending_show_rank',
    'label'    => esc_html__( 'Show Rank Numbers', 'blog-over' ),
    'description' => esc_html__( 'Display "#1, #2, #3 …" position markers beside each post.', 'blog-over' ),
    'section'  => 'blog_over_trending_section',
    'default'  => true,
    'priority' => 21,
    'active_callback' => array(
        array( 'setting' => 'blog_over_trending_enable', 'operator' => '==', 'value' => true ),
    ),
) );

// ═══════════════════════════════════════════
// M) Show Thumbnail
// ═══════════════════════════════════════════
Kirki::add_field( 'Blog_Over_config', array(
    'type'     => 'toggle',
    'settings' => 'blog_over_trending_show_thumb',
    'label'    => esc_html__( 'Show Post Thumbnail', 'blog-over' ),
    'description' => esc_html__( 'Display the featured image as a small square thumbnail beside each post.', 'blog-over' ),
    'section'  => 'blog_over_trending_section',
    'default'  => true,
    'priority' => 22,
    'active_callback' => array(
        array( 'setting' => 'blog_over_trending_enable', 'operator' => '==', 'value' => true ),
    ),
) );

// ═══════════════════════════════════════════
// POST ELEMENTS HEADING
// ═══════════════════════════════════════════
Kirki::add_field( 'Blog_Over_config', array(
    'type'     => 'custom',
    'settings' => 'blog_over_trending_elements_heading',
    'section'  => 'blog_over_trending_section',
    'default'  => '<div style="background:linear-gradient(135deg,#eff6ff,#dbeafe);padding:10px 14px;border-radius:6px;border-left:3px solid #3b82f6;margin-top:20px;"><span style="font-size:13px;font-weight:600;color:#1e40af;text-transform:uppercase;letter-spacing:.5px;">' . esc_html__( 'Post Info Elements', 'blog-over' ) . '</span></div>',
    'priority' => 24,
    'active_callback' => array(
        array( 'setting' => 'blog_over_trending_enable', 'operator' => '==', 'value' => true ),
    ),
) );

// ═══════════════════════════════════════════
// N) Card Elements (sortable)
// ═══════════════════════════════════════════
Kirki::add_field( 'Blog_Over_config', array(
    'type'     => 'sortable',
    'settings' => 'blog_over_trending_elements',
    'label'    => esc_html__( 'Post Info Elements', 'blog-over' ),
    'description' => esc_html__( 'Drag to reorder. Uncheck to hide.', 'blog-over' ),
    'section'  => 'blog_over_trending_section',
    'default'  => array( 'category', 'title', 'meta' ),
    'priority' => 25,
    'choices'  => array(
        'category' => esc_html__( 'Category Badge', 'blog-over' ),
        'title'    => esc_html__( 'Post Title', 'blog-over' ),
        'meta'     => esc_html__( 'Post Meta (date, comments)', 'blog-over' ),
    ),
    'active_callback' => array(
        array( 'setting' => 'blog_over_trending_enable', 'operator' => '==', 'value' => true ),
    ),
) );
