<?php

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * The settings functionality of the plugin.
 *
 * @link       https://thunder-team.com/
 * @since      1.0.0
 *
 * @package    Storyposts
 * @subpackage Storyposts/admin
 */

/**
 * The settings functionality of the plugin.
 *
 * Defines the plugin settings using CMB2.
 *
 * @package    Storyposts
 * @subpackage Storyposts/admin
 * @author     thunder-team
 */
class Storyposts_Settings {

	/**
	 * The ID of this plugin.
	 *
	 * @since    1.0.0
	 * @access   private
	 * @var      string    $plugin_name    The ID of this plugin.
	 */
	private $plugin_name;

	/**
	 * The version of this plugin.
	 *
	 * @since    1.0.0
	 * @access   private
	 * @var      string    $version    The current version of this plugin.
	 */
	private $version;

	/**
	 * Option key
	 * 
	 * @since 1.0.0
	 * @var string
	 */
	protected $key = 'storyposts_options';

	/**
	 * Initialize the class and set its properties.
	 *
	 * @since    1.0.0
	 */
	public function __construct() {

		add_action( 'storyposts_cmb2_admin_init', array( $this, 'register_options_page' ) );

	}

    /**
     * Central registry of all settings defaults.
     * These MUST match the 'default' values set on each CMB2 add_field() call.
     * This prevents the "works only after first save" bug.
     *
     * @return array
     */
    public static function defaults() {
        return [
            // General
            'story_duration'                    => 5000,
            'max_file_size'                     => 10,
            'primary_color'                     => '#93278f',
            // Admin (Collection) Stories
            'collections_per_load'              => 12,
            'collection_stories_layout'         => 'card',
            'collection_slides_per_view_card'   => 8,
            'collection_slides_per_view_avatar' => 12,
            'collection_slide_gap_card'         => 4,
            'collection_slide_gap_avatar'       => 12,
            'collection_max_title_length'       => 30,
            'collection_max_desc_length'        => 120,
            // User Stories
            'enable_user_stories'               => 'on',
            'user_stories_show_on_activity'     => 'on',
            'stories_per_load'                  => 12,
            'require_login_to_view'             => '',
            'user_stories_layout'               => 'card',
            'user_slides_per_view_card'         => 8,
            'user_slides_per_view_avatar'       => 12,
            'user_slide_gap_card'               => 4,
            'user_slide_gap_avatar'             => 12,
            'allowed_types'                     => [ 'text', 'image', 'video' ],
            'max_title_length'                  => 30,
            'max_desc_length'                   => 120,
            'enable_privacy'                    => '',
            'privacy_levels'                    => [ 'public', 'friends', 'private' ],
            'enable_story_expiry'               => '',
            'story_lifespan'                    => 24,
            // Blog Stories
            'blog_story_source'                 => 'category',
            'blog_sources_per_load'             => 12,
            'blog_posts_per_source'             => 5,
            'blog_stories_layout'               => 'card',
            'blog_slides_per_view_card'         => 8,
            'blog_slides_per_view_avatar'       => 12,
            'blog_slide_gap_card'               => 4,
            'blog_slide_gap_avatar'             => 12,
            'blog_max_title_length'             => 30,
            'blog_max_desc_length'              => 120,
            // Product Stories
            'product_sources_per_load'          => 12,
            'product_posts_per_cat'             => 5,
            'product_stories_layout'            => 'card',
            'product_slides_per_view_card'      => 8,
            'product_slides_per_view_avatar'    => 12,
            'product_slide_gap_card'            => 4,
            'product_slide_gap_avatar'          => 12,
        ];
    }

    public static function get( $key = '', $default = false ) {
        // Fetch the entire options array
        $opts = get_option( 'storyposts_options' );

        // If requesting all options
        if ( 'all' === $key ) {
            return is_array( $opts ) ? $opts : [];
        }

        // If the specific key exists in the DB (even if empty/null/false), return it
        if ( is_array( $opts ) && array_key_exists( $key, $opts ) ) {
            return $opts[ $key ];
        }

        // Key not in DB — fall back to the central defaults registry first
        $defaults = self::defaults();
        if ( array_key_exists( $key, $defaults ) ) {
            return $defaults[ $key ];
        }

        // Last resort: use the caller-provided fallback
        return $default;
    }

	/**
	 * Register the options page and fields
	 * 
	 * @since 1.0.0
	 */
	public function register_options_page() {

		$cmb = new_storyposts_cmb2_box( array(
			'id'           => $this->key . '_metabox',
			'title'        => esc_html__( 'Settings', 'storyposts' ),
			'object_types' => array( 'options-page' ),
			'option_key'   => $this->key, // The option key and admin menu page slug.
			'parent_slug'  => 'edit.php?post_type=stpo_story', // Submenu of StoryPosts
			'icon_url'     => 'dashicons-camera', // Menu icon. Only applicable if 'parent_slug' is left empty.
			'display_cb'   => array( $this, 'render_tabs' ), // Custom display callback for tabs
		) );



        // =========================================================================
		// General / Upload Settings (Keeping these for now as global)
		// =========================================================================
		$cmb->add_field( array(
			'name' => esc_html__( 'Global Upload Settings', 'storyposts' ),
			'type' => 'title',
			'id'   => 'global_upload_title',
		) );

		$cmb->add_field( array(
            'name'    => esc_html__( 'Story Duration', 'storyposts' ),
            'desc'    => esc_html__( 'Duration in milliseconds for each story slide (default: 5000). Must be between 1000 and 30000.', 'storyposts' ),
            'id'      => 'story_duration',
            'type'    => 'text',
            'default' => '5000',
            'attributes' => array(
                'type' => 'number',
                'min'  => '1000',
                'max'  => '30000',
                'step' => '500',
            ),
            'sanitization_cb' => function( $value ) {
                $int = absint( $value );
                if ( $int < 1000 ) $int = 1000;
                if ( $int > 30000 ) $int = 30000;
                return $int;
            },
        ) );

		$cmb->add_field( array(
			'name'    => esc_html__( 'Max File Size (MB)', 'storyposts' ),
			'desc'    => esc_html__( 'Maximum allowed file size for story uploads (1-100 MB).', 'storyposts' ),
			'id'      => 'max_file_size',
			'type'    => 'text',
			'default' => '10',
			'attributes' => array(
				'type' => 'number',
				'min'  => '1',
				'max'  => '100',
				'step' => '1',
			),
			'sanitization_cb' => function( $value ) {
				$int = absint( $value );
				if ( $int < 1 ) $int = 1;
				if ( $int > 100 ) $int = 100;
				return $int;
			},
		) );

		// Section: Style Settings
		$cmb->add_field( array(
			'name' => esc_html__( 'Style Settings', 'storyposts' ),
			'type' => 'title',
			'id'   => 'style_settings_title',
		) );

		$cmb->add_field( array(
			'name'    => esc_html__( 'Primary Color', 'storyposts' ),
			'desc'    => esc_html__( 'Choose the primary color for StoryPosts (buttons, progress bars, etc.).', 'storyposts' ),
			'id'      => 'primary_color',
			'type'    => 'colorpicker',
			'default' => '#93278f',
		) );


		// =========================================================================
		// Admin Stories (Collections) Settings
		// =========================================================================
		// Section: General
		$cmb->add_field( array(
			'name' => esc_html__( 'Admin Stories - General Settings', 'storyposts' ),
			'type' => 'title',
			'id'   => 'collection_stories_general_title',
		) );

		$cmb->add_field( array(
			'name'    => esc_html__( 'Story Cards Per Load', 'storyposts' ),
			'desc'    => esc_html__( 'Number of story cards to load per chunk (5-30).', 'storyposts' ),
			'id'      => 'collections_per_load',
			'type'    => 'text',
			'default' => '12',
			'attributes' => array(
				'type' => 'number',
				'min'  => '5',
				'max'  => '30',
				'step' => '1',
			),
			'sanitization_cb' => function( $value ) {
				$int = absint( $value );
				if ( $int < 5 ) $int = 5;
				if ( $int > 30 ) $int = 30;
				return $int;
			},
		) );

		// Section: Appearance
		$cmb->add_field( array(
			'name' => esc_html__( 'Admin Stories - Appearance', 'storyposts' ),
			'type' => 'title',
			'id'   => 'collection_stories_appearance_title',
		) );

		$cmb->add_field( array(
			'name'    => esc_html__( 'Slider Layout', 'storyposts' ),
			'desc'    => esc_html__( 'Choose the visual style for the collection story slider.', 'storyposts' ),
			'id'      => 'collection_stories_layout',
			'type'    => 'radio',
			'options' => array(
				'card'   => esc_html__( 'Card Layout (Facebook-style)', 'storyposts' ),
				'avatar' => esc_html__( 'Avatar Layout (Instagram-style)', 'storyposts' ),
			),
			'default' => 'card',
		) );

		$cmb->add_field( array(
			'name'    => esc_html__( 'Slides Per View', 'storyposts' ),
			'desc'    => esc_html__( 'Number of collection cards visible at once (default: 5).', 'storyposts' ),
			'id'      => 'collection_slides_per_view_card',
			'type'    => 'text',
			'default' => '8',
			'attributes' => array(
				'type' => 'number',
				'min'  => '1',
				'data-conditional-id'    => 'collection_stories_layout',
				'data-conditional-value' => 'card',
			),
		) );

		$cmb->add_field( array(
			'name'    => esc_html__( 'Slides Per View', 'storyposts' ),
			'desc'    => esc_html__( 'Number of collection avatars visible at once (default: 12).', 'storyposts' ),
			'id'      => 'collection_slides_per_view_avatar',
			'type'    => 'text',
			'default' => '12',
			'attributes' => array(
				'type' => 'number',
				'min'  => '1',
				'data-conditional-id'    => 'collection_stories_layout',
				'data-conditional-value' => 'avatar',
			),
		) );

		$cmb->add_field( array(
			'name'    => esc_html__( 'Slide Gap (px)', 'storyposts' ),
			'desc'    => esc_html__( 'Space between collection cards in pixels (default: 4).', 'storyposts' ),
			'id'      => 'collection_slide_gap_card',
			'type'    => 'text',
			'default' => '4',
			'attributes' => array(
				'type' => 'number',
				'min'  => '0',
				'data-conditional-id'    => 'collection_stories_layout',
				'data-conditional-value' => 'card',
			),
		) );

		$cmb->add_field( array(
			'name'    => esc_html__( 'Slide Gap (px)', 'storyposts' ),
			'desc'    => esc_html__( 'Space between collection avatars in pixels (default: 12).', 'storyposts' ),
			'id'      => 'collection_slide_gap_avatar',
			'type'    => 'text',
			'default' => '12',
			'attributes' => array(
				'type' => 'number',
				'min'  => '0',
				'data-conditional-id'    => 'collection_stories_layout',
				'data-conditional-value' => 'avatar',
			),
		) );

		// Section: Content & Constraints
		$cmb->add_field( array(
			'name' => esc_html__( 'Admin Stories - Content & Constraints', 'storyposts' ),
			'type' => 'title',
			'id'   => 'collection_stories_content_title',
		) );

		$cmb->add_field( array(
			'name'    => esc_html__( 'Max Title Length', 'storyposts' ),
			'desc'    => esc_html__( 'Maximum number of characters for the collection story title.', 'storyposts' ),
			'id'      => 'collection_max_title_length',
			'type'    => 'text',
			'default' => '30',
			'attributes' => array(
				'type' => 'number',
				'min'  => '1',
			),
		) );

		$cmb->add_field( array(
			'name'    => esc_html__( 'Max Description Length', 'storyposts' ),
			'desc'    => esc_html__( 'Maximum number of characters for the collection story description/caption.', 'storyposts' ),
			'id'      => 'collection_max_desc_length',
			'type'    => 'text',
			'default' => '120',
			'attributes' => array(
				'type' => 'number',
				'min'  => '1',
			),
		) );

		if ( storyposts_component_exists( 'user-stories' ) ) {

            // =========================================================================
            // User Stories Settings
            // =========================================================================
            $cmb->add_field( array(
                'name' => esc_html__( 'User Stories - General Settings', 'storyposts' ),
                'type' => 'title',
                'id'   => 'user_stories_general_title',
            ) );

            // 0. Enable User Stories
            $cmb->add_field( array(
                'name'    => esc_html__( 'Enable User Stories', 'storyposts' ),
                'desc'    => esc_html__( 'Enable or disable the user stories functionality.', 'storyposts' ),
                'id'      => 'enable_user_stories',
                'type'    => 'radio',
                'options' => array(
                    'on'  => esc_html__( 'On', 'storyposts' ),
                    'off' => esc_html__( 'Off', 'storyposts' ),
                ),
                'default' => 'on',
            ) );

            if ( function_exists( 'bp_is_active' ) ) {
                $cmb->add_field( array(
                    'name'    => esc_html__( 'Show on BuddyPress Activity Page', 'storyposts' ),
                    'desc'    => esc_html__( 'Display the user story slider on the main BuddyPress activity directory.', 'storyposts' ),
                    'id'      => 'user_stories_show_on_activity',
                    'type'    => 'radio',
                    'options' => array(
                        'on'  => esc_html__( 'On', 'storyposts' ),
                        'off' => esc_html__( 'Off', 'storyposts' ),
                    ),
                    'default' => 'on',
                    'attributes' => array(
                        'data-conditional-id'    => 'enable_user_stories',
                        'data-conditional-value' => 'on',
                    ),
                ) );
            }

            $cmb->add_field( array(
                'name'    => esc_html__( 'Story Cards Per Load', 'storyposts' ),
                'desc'    => esc_html__( 'Number of story cards to load per chunk (5-30). Lower = faster initial load, higher = fewer requests.', 'storyposts' ),
                'id'      => 'stories_per_load',
                'type'    => 'text',
                'default' => '12',
                'attributes' => array(
                    'type' => 'number',
                    'min'  => '5',
                    'max'  => '30',
                    'step' => '1',
                ),
                'sanitization_cb' => function( $value ) {
                    $int = absint( $value );
                    if ( $int < 5 ) $int = 5;
                    if ( $int > 30 ) $int = 30;
                    return $int;
                },
            ) );

            $cmb->add_field( array(
                'name'    => esc_html__( 'Require Login to View Stories', 'storyposts' ),
                'desc'    => esc_html__( 'Only logged-in users can view stories. When disabled, guests can view public stories.', 'storyposts' ),
                'id'      => 'require_login_to_view',
                'type'    => 'checkbox',
            ) );

            // Section: Appearance
            $cmb->add_field( array(
                'name' => esc_html__( 'User Stories - Appearance', 'storyposts' ),
                'type' => 'title',
                'id'   => 'user_stories_appearance_title',
            ) );

            // Slider Appearance (User Stories)
            $cmb->add_field( array(
                'name'    => esc_html__( 'Slider Layout', 'storyposts' ),
                'desc'    => esc_html__( 'Choose the visual style for the story slider.', 'storyposts' ),
                'id'      => 'user_stories_layout',
                'type'    => 'radio',
                'options' => array(
                    'card'   => esc_html__( 'Card Layout (Facebook-style)', 'storyposts' ),
                    'avatar' => esc_html__( 'Avatar Layout (Instagram-style)', 'storyposts' ),
                ),
                'default' => 'card',
            ) );

            $cmb->add_field( array(
                'name'    => esc_html__( 'Slides Per View', 'storyposts' ),
                'desc'    => esc_html__( 'Number of user story cards visible at once (default: 5).', 'storyposts' ),
                'id'      => 'user_slides_per_view_card',
                'type'    => 'text',
                'default' => '8',
                'attributes' => array(
                    'type' => 'number',
                    'min'  => '1',
                    'data-conditional-id'    => 'user_stories_layout',
                    'data-conditional-value' => 'card',
                ),
            ) );

            $cmb->add_field( array(
                'name'    => esc_html__( 'Slides Per View', 'storyposts' ),
                'desc'    => esc_html__( 'Number of user story avatars visible at once (default: 12).', 'storyposts' ),
                'id'      => 'user_slides_per_view_avatar',
                'type'    => 'text',
                'default' => '12',
                'attributes' => array(
                    'type' => 'number',
                    'min'  => '1',
                    'data-conditional-id'    => 'user_stories_layout',
                    'data-conditional-value' => 'avatar',
                ),
            ) );

            $cmb->add_field( array(
                'name'    => esc_html__( 'Slide Gap (px)', 'storyposts' ),
                'desc'    => esc_html__( 'Space between user story cards in pixels (default: 4).', 'storyposts' ),
                'id'      => 'user_slide_gap_card',
                'type'    => 'text',
                'default' => '4',
                'attributes' => array(
                    'type' => 'number',
                    'min'  => '0',
                    'data-conditional-id'    => 'user_stories_layout',
                    'data-conditional-value' => 'card',
                ),
            ) );

            $cmb->add_field( array(
                'name'    => esc_html__( 'Slide Gap (px)', 'storyposts' ),
                'desc'    => esc_html__( 'Space between user story avatars in pixels (default: 12).', 'storyposts' ),
                'id'      => 'user_slide_gap_avatar',
                'type'    => 'text',
                'default' => '12',
                'attributes' => array(
                    'type' => 'number',
                    'min'  => '0',
                    'data-conditional-id'    => 'user_stories_layout',
                    'data-conditional-value' => 'avatar',
                ),
            ) );

            // Section: Content & Constraints
            $cmb->add_field( array(
                'name' => esc_html__( 'User Stories - Content & Constraints', 'storyposts' ),
                'type' => 'title',
                'id'   => 'user_stories_content_title',
            ) );


            // 1. Story Types
            $cmb->add_field( array(
                'name'    => esc_html__( 'Allowed Story Types', 'storyposts' ),
                'desc'    => esc_html__( 'Select which types of stories users can create.', 'storyposts' ),
                'id'      => 'allowed_types',
                'type'    => 'multicheck',
                'options' => array(
                    'text'  => esc_html__( 'Text', 'storyposts' ),
                    'image' => esc_html__( 'Image', 'storyposts' ),
                    'video' => esc_html__( 'Video', 'storyposts' ),
                ),
                'default' => array( 'text', 'image', 'video' ),
            ) );

            // 2. Story Title Length
            $cmb->add_field( array(
                'name'    => esc_html__( 'Max Title Length', 'storyposts' ),
                'desc'    => esc_html__( 'Maximum number of characters for the story title.', 'storyposts' ),
                'id'      => 'max_title_length',
                'type'    => 'text',
                'default' => '30',
                'attributes' => array(
                    'type' => 'number',
                    'min'  => '1',
                ),
            ) );

            // 3. Story Description Length
            $cmb->add_field( array(
                'name'    => esc_html__( 'Max Description Length', 'storyposts' ),
                'desc'    => esc_html__( 'Maximum number of characters for the story description/caption.', 'storyposts' ),
                'id'      => 'max_desc_length',
                'type'    => 'text',
                'default' => '120',
                'attributes' => array(
                    'type' => 'number',
                    'min'  => '1',
                ),
            ) );

            // Section: Expiry & Privacy
            $cmb->add_field( array(
                'name' => esc_html__( 'User Stories - Expiry & Privacy', 'storyposts' ),
                'type' => 'title',
                'id'   => 'user_stories_expiry_privacy_title',
            ) );

            // 4. Enable Privacy
            $cmb->add_field( array(
                'name'    => esc_html__( 'Enable Privacy Settings', 'storyposts' ),
                'desc'    => esc_html__( 'Allow users to choose who can see their story.', 'storyposts' ),
                'id'      => 'enable_privacy',
                'type'    => 'checkbox',
            ) );

            // 5. Privacy Types (Conditional)
            $cmb->add_field( array(
                'name'    => esc_html__( 'Allowed Privacy Levels', 'storyposts' ),
                'desc'    => esc_html__( 'Select which privacy options are available to users.', 'storyposts' ),
                'id'      => 'privacy_levels',
                'type'    => 'multicheck',
                'options' => array(
                    'public'  => esc_html__( 'Public', 'storyposts' ),
                    'friends' => esc_html__( 'Friends Only', 'storyposts' ),
                    'private' => esc_html__( 'Only Me', 'storyposts' ),
                ),
                'default' => array( 'public', 'friends', 'private' ),
                'attributes' => array(
                    'data-conditional-id'    => 'enable_privacy',
                    'data-conditional-value' => 'on',
                ),
            ) );

            $cmb->add_field( array(
                'name'    => esc_html__( 'Enable Story Expiry', 'storyposts' ),
                'desc'    => esc_html__( 'Automatically expire stories after a set time period.', 'storyposts' ),
                'id'      => 'enable_story_expiry',
                'type'    => 'checkbox',
            ) );

            $cmb->add_field( array(
                'name'    => esc_html__( 'Story Lifespan (hours)', 'storyposts' ),
                'desc'    => esc_html__( 'How long stories remain visible before expiring (default: 24 hours).', 'storyposts' ),
                'id'      => 'story_lifespan',
                'type'    => 'text',
                'default' => '24',
                'attributes' => array(
                    'type' => 'number',
                    'min'  => '1',
                    'data-conditional-id' => 'enable_story_expiry',
                    'data-conditional-value' => 'on',
                ),
            ) );

		} // end User Stories

		if ( storyposts_component_exists( 'blog-stories' ) ) {

            // =========================================================================
            // Blog Stories Settings
            // =========================================================================
            // Section: General
            $cmb->add_field( array(
                'name' => esc_html__( 'Blog Stories - General Settings', 'storyposts' ),
                'type' => 'title',
                'id'   => 'blog_stories_general_title',
            ) );

            $cmb->add_field( array(
                'name'    => esc_html__( 'Story Source', 'storyposts' ),
                'desc'    => esc_html__( 'Default source for blog stories.', 'storyposts' ),
                'id'      => 'blog_story_source',
                'type'    => 'select',
                'options' => array(
                    'category' => esc_html__( 'Categories', 'storyposts' ),
                    'author'   => esc_html__( 'Authors', 'storyposts' ),
                ),
                'default' => 'category',
            ) );

            $cmb->add_field( array(
                'name'    => esc_html__( 'Story Cards Per Load', 'storyposts' ),
                'desc'    => esc_html__( 'Number of story cards to load per chunk (5-30). ', 'storyposts' ),
                'id'      => 'blog_sources_per_load',
                'type'    => 'text',
                'default' => '12',
                'attributes' => array(
                    'type' => 'number',
                    'min'  => '5',
                    'max'  => '30',
                    'step' => '1',
                ),
                'sanitization_cb' => function( $value ) {
                    $int = absint( $value );
                    if ( $int < 5 ) $int = 5;
                    if ( $int > 30 ) $int = 30;
                    return $int;
                },
            ) );

            $cmb->add_field( array(
                'name'    => esc_html__( 'Stories Per Card', 'storyposts' ),
                'desc'    => esc_html__( 'Number of latest posts to fetch per category/author (default: 5).', 'storyposts' ),
                'id'      => 'blog_posts_per_source',
                'type'    => 'text',
                'default' => '5',
                'attributes' => array(
                    'type' => 'number',
                    'min'  => '1',
                    'max'  => '20',
                    'step' => '1',
                ),
                'sanitization_cb' => function( $value ) {
                    $int = absint( $value );
                    if ( $int < 1 ) $int = 1;
                    if ( $int > 20 ) $int = 20;
                    return $int;
                },
            ) );

            // Section: Appearance
            $cmb->add_field( array(
                'name' => esc_html__( 'Blog Stories - Appearance', 'storyposts' ),
                'type' => 'title',
                'id'   => 'blog_stories_appearance_title',
            ) );

            $cmb->add_field( array(
                'name'    => esc_html__( 'Slider Layout', 'storyposts' ),
                'desc'    => esc_html__( 'Choose the visual style for the blog story slider.', 'storyposts' ),
                'id'      => 'blog_stories_layout',
                'type'    => 'radio',
                'options' => array(
                    'card'   => esc_html__( 'Card Layout (Facebook-style)', 'storyposts' ),
                    'avatar' => esc_html__( 'Avatar Layout (Instagram-style)', 'storyposts' ),
                ),
                'default' => 'card',
            ) );

            $cmb->add_field( array(
                'name'    => esc_html__( 'Slides Per View', 'storyposts' ),
                'desc'    => esc_html__( 'Number of blog story cards visible at once (default: 5).', 'storyposts' ),
                'id'      => 'blog_slides_per_view_card',
                'type'    => 'text',
                'default' => '8',
                'attributes' => array(
                    'type' => 'number',
                    'min'  => '1',
                    'data-conditional-id'    => 'blog_stories_layout',
                    'data-conditional-value' => 'card',
                ),
            ) );

            $cmb->add_field( array(
                'name'    => esc_html__( 'Slides Per View', 'storyposts' ),
                'desc'    => esc_html__( 'Number of blog story avatars visible at once (default: 12).', 'storyposts' ),
                'id'      => 'blog_slides_per_view_avatar',
                'type'    => 'text',
                'default' => '12',
                'attributes' => array(
                    'type' => 'number',
                    'min'  => '1',
                    'data-conditional-id'    => 'blog_stories_layout',
                    'data-conditional-value' => 'avatar',
                ),
            ) );

            $cmb->add_field( array(
                'name'    => esc_html__( 'Slide Gap (px)', 'storyposts' ),
                'desc'    => esc_html__( 'Space between blog story cards in pixels (default: 4).', 'storyposts' ),
                'id'      => 'blog_slide_gap_card',
                'type'    => 'text',
                'default' => '4',
                'attributes' => array(
                    'type' => 'number',
                    'min'  => '0',
                    'data-conditional-id'    => 'blog_stories_layout',
                    'data-conditional-value' => 'card',
                ),
            ) );

            $cmb->add_field( array(
                'name'    => esc_html__( 'Slide Gap (px)', 'storyposts' ),
                'desc'    => esc_html__( 'Space between blog story avatars in pixels (default: 12).', 'storyposts' ),
                'id'      => 'blog_slide_gap_avatar',
                'type'    => 'text',
                'default' => '12',
                'attributes' => array(
                    'type' => 'number',
                    'min'  => '0',
                    'data-conditional-id'    => 'blog_stories_layout',
                    'data-conditional-value' => 'avatar',
                ),
            ) );

            // Section: Content & Constraints
            $cmb->add_field( array(
                'name' => esc_html__( 'Blog Stories - Content & Constraints', 'storyposts' ),
                'type' => 'title',
                'id'   => 'blog_stories_content_title',
            ) );

            $cmb->add_field( array(
                'name'    => esc_html__( 'Max Title Length', 'storyposts' ),
                'desc'    => esc_html__( 'Maximum number of characters for the blog story title.', 'storyposts' ),
                'id'      => 'blog_max_title_length',
                'type'    => 'text',
                'default' => '30',
                'attributes' => array(
                    'type' => 'number',
                    'min'  => '1',
                ),
            ) );

            $cmb->add_field( array(
                'name'    => esc_html__( 'Max Description Length', 'storyposts' ),
                'desc'    => esc_html__( 'Maximum number of characters for the blog story description/caption.', 'storyposts' ),
                'id'      => 'blog_max_desc_length',
                'type'    => 'text',
                'default' => '120',
                'attributes' => array(
                    'type' => 'number',
                    'min'  => '1',
                ),
            ) );

		} // end Blog Stories

		if ( storyposts_component_exists( 'product-stories' ) ) {

            // =========================================================================
            // Product Stories Settings
            // =========================================================================
            // Section: General
            $cmb->add_field( array(
                'name' => esc_html__( 'Product Stories - General Settings', 'storyposts' ),
                'type' => 'title',
                'id'   => 'product_stories_general_title',
            ) );

            $cmb->add_field( array(
                'name'    => esc_html__( 'Story Cards Per Load', 'storyposts' ),
                'desc'    => esc_html__( 'Number of product categories to load per chunk (5-30).', 'storyposts' ),
                'id'      => 'product_sources_per_load',
                'type'    => 'text',
                'default' => '12',
                'attributes' => array(
                    'type' => 'number',
                    'min'  => '5',
                    'max'  => '30',
                    'step' => '1',
                ),
                'sanitization_cb' => function( $value ) {
                    $int = absint( $value );
                    if ( $int < 5 ) $int = 5;
                    if ( $int > 30 ) $int = 30;
                    return $int;
                },
            ) );

            $cmb->add_field( array(
                'name'    => esc_html__( 'Stories Per Card', 'storyposts' ),
                'desc'    => esc_html__( 'Number of latest products to fetch per category (default: 5).', 'storyposts' ),
                'id'      => 'product_posts_per_cat',
                'type'    => 'text',
                'default' => '5',
                'attributes' => array(
                    'type' => 'number',
                    'min'  => '1',
                    'max'  => '20',
                    'step' => '1',
                ),
                'sanitization_cb' => function( $value ) {
                    $int = absint( $value );
                    if ( $int < 1 ) $int = 1;
                    if ( $int > 20 ) $int = 20;
                    return $int;
                },
            ) );

            // Section: Appearance
            $cmb->add_field( array(
                'name' => esc_html__( 'Product Stories - Appearance', 'storyposts' ),
                'type' => 'title',
                'id'   => 'product_stories_appearance_title',
            ) );

            $cmb->add_field( array(
                'name'    => esc_html__( 'Slider Layout', 'storyposts' ),
                'desc'    => esc_html__( 'Choose the visual style for the product story slider.', 'storyposts' ),
                'id'      => 'product_stories_layout',
                'type'    => 'radio',
                'options' => array(
                    'card'   => esc_html__( 'Card Layout (Facebook-style)', 'storyposts' ),
                    'avatar' => esc_html__( 'Avatar Layout (Instagram-style)', 'storyposts' ),
                ),
                'default' => 'card',
            ) );

            $cmb->add_field( array(
                'name'    => esc_html__( 'Slides Per View', 'storyposts' ),
                'desc'    => esc_html__( 'Number of product story cards visible at once (default: 5).', 'storyposts' ),
                'id'      => 'product_slides_per_view_card',
                'type'    => 'text',
                'default' => '8',
                'attributes' => array(
                    'type' => 'number',
                    'min'  => '1',
                    'data-conditional-id'    => 'product_stories_layout',
                    'data-conditional-value' => 'card',
                ),
            ) );

            $cmb->add_field( array(
                'name'    => esc_html__( 'Slides Per View', 'storyposts' ),
                'desc'    => esc_html__( 'Number of product story avatars visible at once (default: 12).', 'storyposts' ),
                'id'      => 'product_slides_per_view_avatar',
                'type'    => 'text',
                'default' => '12',
                'attributes' => array(
                    'type' => 'number',
                    'min'  => '1',
                    'data-conditional-id'    => 'product_stories_layout',
                    'data-conditional-value' => 'avatar',
                ),
            ) );

            $cmb->add_field( array(
                'name'    => esc_html__( 'Slide Gap (px)', 'storyposts' ),
                'desc'    => esc_html__( 'Space between product story cards in pixels (default: 4).', 'storyposts' ),
                'id'      => 'product_slide_gap_card',
                'type'    => 'text',
                'default' => '4',
                'attributes' => array(
                    'type' => 'number',
                    'min'  => '0',
                    'data-conditional-id'    => 'product_stories_layout',
                    'data-conditional-value' => 'card',
                ),
            ) );

            $cmb->add_field( array(
                'name'    => esc_html__( 'Slide Gap (px)', 'storyposts' ),
                'desc'    => esc_html__( 'Space between product story avatars in pixels (default: 12).', 'storyposts' ),
                'id'      => 'product_slide_gap_avatar',
                'type'    => 'text',
                'default' => '12',
                'attributes' => array(
                    'type' => 'number',
                    'min'  => '0',
                    'data-conditional-id'    => 'product_stories_layout',
                    'data-conditional-value' => 'avatar',
                ),
            ) );

		} // end Product Stories

	}

	/**
	 * Custom display callback for tabbed settings
	 * 
	 * @param array $hookup CMB2 hookup array
	 */
	public function render_tabs( $hookup ) {
		?>
		<div class="wrap cmb2-options-page <?php echo esc_attr( $hookup->cmb->cmb_id ); ?>">
			<?php if ( $hookup->cmb->prop( 'title' ) ) : ?>
				<h2><?php echo wp_kses_post( $hookup->cmb->prop( 'title' ) ); ?></h2>
			<?php endif; ?>

			<h2 class="nav-tab-wrapper">
				<a href="#tab-general" class="nav-tab nav-tab-active"><?php esc_html_e( 'General', 'storyposts' ); ?></a>
				<a href="#tab-collection-stories" class="nav-tab"><?php esc_html_e( 'Admin Stories (Collections)', 'storyposts' ); ?></a>
				<?php if ( storyposts_component_exists( 'user-stories' ) ) : ?>
				<a href="#tab-user-stories" class="nav-tab"><?php esc_html_e( 'User Stories', 'storyposts' ); ?></a>
				<?php endif; ?>
				<?php if ( storyposts_component_exists( 'blog-stories' ) ) : ?>
				<a href="#tab-blog-stories" class="nav-tab"><?php esc_html_e( 'Blog Stories', 'storyposts' ); ?></a>
				<?php endif; ?>
				<?php if ( storyposts_component_exists( 'product-stories' ) ) : ?>
				<a href="#tab-product-stories" class="nav-tab"><?php esc_html_e( 'Product Stories', 'storyposts' ); ?></a>
				<?php endif; ?>
			</h2>

			<form class="cmb-form" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>" method="POST" id="<?php echo esc_attr( $hookup->cmb->cmb_id ); ?>" enctype="multipart/form-data" encoding="multipart/form-data">
				<input type="hidden" name="action" value="<?php echo esc_attr( $hookup->option_key ); ?>">
				<?php $hookup->cmb->nonce_field(); ?>
				
				<?php
				// Render all fields using CMB2's built-in method
				$hookup->cmb->show_form( $hookup->option_key, 'options-page' );
				?>
				
				<?php submit_button( esc_attr( $hookup->cmb->prop( 'save_button', esc_html__( 'Save Settings', 'storyposts' ) ) ), 'primary', 'submit-cmb' ); ?>

			</form>
		</div>
		<?php
	}
}
