<?php
/**
 * Peblog Functions
 *
 * @package Peblog
 */

// Prevent direct access.
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

// Theme setup
function peblog_setup() {
	// Add theme support for various features
	add_theme_support( 'automatic-feed-links' );
	add_theme_support( 'post-thumbnails' );
	add_theme_support(
		'post-formats',
		array(
			'aside',
			'image',
			'video',
			'quote',
			'link',
			'gallery',
			'audio',
			'status',
			'chat',
		)
	);
	add_theme_support( 'title-tag' );
	add_theme_support(
		'html5',
		array(
			'search-form',
			'comment-form',
			'comment-list',
			'gallery',
			'caption',
			'style',
			'script',
		)
	);
	add_theme_support( 'custom-logo' );
	add_theme_support( 'customize-selective-refresh-widgets' );
	add_theme_support( 'responsive-embeds' );

	// Add support for editor styles
	add_theme_support( 'editor-styles' );
	add_editor_style( 'assets/css/style.css' );

	// Add support for wide and full alignment
	add_theme_support( 'align-wide' );

	// Add support for block styles
	add_theme_support( 'wp-block-styles' );

	// Add support for custom color palette
	add_theme_support(
		'editor-color-palette',
		array(
			array(
				'name'  => __( 'Primary', 'peblog' ),
				'slug'  => 'primary',
				'color' => '#6366f1',
			),
			array(
				'name'  => __( 'Secondary', 'peblog' ),
				'slug'  => 'secondary',
				'color' => '#8b5cf6',
			),
			array(
				'name'  => __( 'Success', 'peblog' ),
				'slug'  => 'success',
				'color' => '#10b981',
			),
			array(
				'name'  => __( 'Warning', 'peblog' ),
				'slug'  => 'warning',
				'color' => '#f59e0b',
			),
			array(
				'name'  => __( 'Danger', 'peblog' ),
				'slug'  => 'danger',
				'color' => '#ef4444',
			),
		)
	);
}
add_action( 'after_setup_theme', 'peblog_setup' );

// Favicon is now handled automatically by WordPress Site Icon setting in Customizer (WordPress 4.3+)

// Get placeholder icon based on post format
function peblog_get_placeholder_icon( $post_id = null ) {
	if ( ! $post_id ) {
		$post_id = get_the_ID();
	}

	$post_format = get_post_format( $post_id );
	$icon_path   = get_template_directory_uri() . '/assets/images/icon/document.svg';

	// Map post formats to icons
	$format_icons = array(
		'image'   => 'document.svg',
		'video'   => 'video.svg',
		'audio'   => 'audio.svg',
		'gallery' => 'gallery.svg',
		'quote'   => 'quote.svg',
		'link'    => 'link.svg',
		'aside'   => 'document.svg',
		'status'  => 'document.svg',
		'chat'    => 'chat.svg',
	);

	if ( $post_format && isset( $format_icons[ $post_format ] ) ) {
		$icon_path = get_template_directory_uri() . '/assets/images/icon/' . $format_icons[ $post_format ];
	}

	return $icon_path;
}

// Custom Menu Walker for Nested Menus
class Peblog_Walker_Nav_Menu extends Walker_Nav_Menu {

	// Start Level (ul)
	function start_lvl( &$output, $depth = 0, $args = null ) {
		$indent  = str_repeat( "\t", $depth );
		$output .= "\n$indent<ul class=\"sub-menu\">\n";
	}

	// End Level (ul)
	function end_lvl( &$output, $depth = 0, $args = null ) {
		$indent  = str_repeat( "\t", $depth );
		$output .= "$indent</ul>\n";
	}

	// Start Element (li)
	function start_el( &$output, $item, $depth = 0, $args = null, $id = 0 ) {
		$indent = ( $depth ) ? str_repeat( "\t", $depth ) : '';

		$classes   = empty( $item->classes ) ? array() : (array) $item->classes;
		$classes[] = 'menu-item-' . $item->ID;

		// Add dropdown class for parent items
		if ( in_array( 'menu-item-has-children', $classes ) ) {
			$classes[] = 'has-dropdown';
		}

		$class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item, $args ) );
		$class_names = $class_names ? ' class="' . esc_attr( $class_names ) . '"' : '';

		$id = apply_filters( 'nav_menu_item_id', 'menu-item-' . $item->ID, $item, $args );
		$id = $id ? ' id="' . esc_attr( $id ) . '"' : '';

		$output .= $indent . '<li' . $id . $class_names . '>';

		$attributes  = ! empty( $item->attr_title ) ? ' title="' . esc_attr( $item->attr_title ) . '"' : '';
		$attributes .= ! empty( $item->target ) ? ' target="' . esc_attr( $item->target ) . '"' : '';
		$attributes .= ! empty( $item->xfn ) ? ' rel="' . esc_attr( $item->xfn ) . '"' : '';
		$attributes .= ! empty( $item->url ) ? ' href="' . esc_attr( $item->url ) . '"' : '';

		$item_output  = isset( $args->before ) ? $args->before : '';
		$item_output .= '<a' . $attributes . '>';
		$item_output .= ( isset( $args->link_before ) ? $args->link_before : '' ) . esc_html( apply_filters( 'the_title', $item->title, $item->ID ) ) . ( isset( $args->link_after ) ? $args->link_after : '' );

		// Add dropdown arrow for parent items
		if ( in_array( 'menu-item-has-children', $classes ) ) {
			$item_output .= ' <span class="dropdown-arrow">
                <svg width="16px" height="16px" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
                    <path d="M19 9L14 14.1599C13.7429 14.4323 13.4329 14.6493 13.089 14.7976C12.7451 14.9459 12.3745 15.0225 12 15.0225C11.6255 15.0225 11.2549 14.9459 10.9109 14.7976C10.567 14.6493 10.2571 14.4323 10 14.1599L5 9" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"/>
                </svg>
            </span>';
		}

		$item_output .= '</a>';
		$item_output .= isset( $args->after ) ? $args->after : '';

		$output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
	}

	// End Element (li)
	function end_el( &$output, $item, $depth = 0, $args = null ) {
		$output .= "</li>\n";
	}
}

// Enqueue styles and scripts
function peblog_enqueue_assets() {
	// Main theme stylesheet
	wp_enqueue_style( 'peblog-style', get_stylesheet_uri(), array(), '1.0.0' );

	// Original CSS files
	wp_enqueue_style( 'peblog-original-css', get_template_directory_uri() . '/assets/css/style.css', array(), '1.0.0' );
	wp_enqueue_style( 'peblog-responsive-css', get_template_directory_uri() . '/assets/css/responsive.css', array( 'peblog-original-css' ), '1.0.0' );

	// Swiper CSS - Minified version (original also included in assets/css/vendor/swiper-bundle.css)
	$swiper_css_path = get_template_directory() . '/assets/css/vendor/swiper-bundle.min.css';
	if ( file_exists( $swiper_css_path ) ) {
		wp_enqueue_style( 'swiper-css', get_template_directory_uri() . '/assets/css/vendor/swiper-bundle.min.css', array(), '9.0.0' );
	}

	// Swiper JavaScript - Minified version (original also included in assets/js/vendor/swiper-bundle.js)
	$swiper_js_path = get_template_directory() . '/assets/js/vendor/swiper-bundle.min.js';
	$swiper_deps    = array( 'jquery' );
	if ( file_exists( $swiper_js_path ) ) {
		wp_enqueue_script( 'swiper-js', get_template_directory_uri() . '/assets/js/vendor/swiper-bundle.min.js', array(), '9.0.0', true );
		$swiper_deps[] = 'swiper-js';
	}

	// Main JavaScript (load after Swiper if available)
	wp_enqueue_script( 'peblog-script', get_template_directory_uri() . '/assets/js/main.js', $swiper_deps, '1.0.0', true );

	// Localize script for AJAX and theme settings
	wp_localize_script(
		'peblog-script',
		'peblog_ajax',
		array(
			'ajax_url' => admin_url( 'admin-ajax.php' ),
			'nonce'    => wp_create_nonce( 'peblog_nonce' ),
		)
	);

	// Enqueue comment reply script on singular posts with comments open
	if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) {
		wp_enqueue_script( 'comment-reply' );
	}
}
add_action( 'wp_enqueue_scripts', 'peblog_enqueue_assets' );

// Register navigation menus
function peblog_register_menus() {
	register_nav_menus(
		array(
			'primary' => __( 'Primary Menu', 'peblog' ),
			'footer'  => __( 'Footer Menu', 'peblog' ),
		)
	);
}
add_action( 'init', 'peblog_register_menus' );

// Register widget areas
function peblog_widgets_init() {
	register_sidebar(
		array(
			'name'          => __( 'Sidebar', 'peblog' ),
			'id'            => 'peblog-sidebar',
			'description'   => __( 'Add widgets here.', 'peblog' ),
			'before_widget' => '<section id="%1$s" class="widget sidebar-widget %2$s">',
			'after_widget'  => '</section>',
			'before_title'  => '<h3 class="widget-title">',
			'after_title'   => '</h3>',
		)
	);
}
add_action( 'widgets_init', 'peblog_widgets_init' );

// Template Part Hooks
// These functions render the default template parts
// Premium/Child themes can use remove_action() and add_action() to replace them

function peblog_render_hero_slider() {
	get_template_part( 'template-parts/hero/hero-slider' );
}
add_action( 'peblog_hero_slider', 'peblog_render_hero_slider', 10 );

/**
 * Render featured posts section.
 */
function peblog_render_featured_posts() {
	get_template_part( 'template-parts/featured-posts/featured-posts' );
}
add_action( 'peblog_featured_posts', 'peblog_render_featured_posts', 10 );

/**
 * Render latest posts slider section.
 */
function peblog_render_latest_posts() {
	get_template_part( 'template-parts/latest-posts/latest-posts' );
}
add_action( 'peblog_latest_posts', 'peblog_render_latest_posts', 10 );

/**
 * Render most read posts section.
 */
function peblog_render_most_read() {
	get_template_part( 'template-parts/most-read/most-read' );
}
add_action( 'peblog_most_read', 'peblog_render_most_read', 10 );

/**
 * Render category filter section.
 */
function peblog_render_category_filter() {
	get_template_part( 'template-parts/category-filter/category-filter' );
}
add_action( 'peblog_category_filter', 'peblog_render_category_filter', 10 );

// Custom post types removed - using standard WordPress posts only

// Custom excerpt length
function peblog_excerpt_length( $length ) {
	// Don't modify excerpt in admin area
	if ( is_admin() ) {
		return $length;
	}
	return 20;
}
add_filter( 'excerpt_length', 'peblog_excerpt_length' );

// Custom excerpt more
function peblog_excerpt_more( $more ) {
	// Don't modify excerpt in admin area
	if ( is_admin() ) {
		return $more;
	}
	return __( '...', 'peblog' );
}
add_filter( 'excerpt_more', 'peblog_excerpt_more' );

// Add custom body classes
function peblog_body_classes( $classes ) {
	if ( is_home() || is_front_page() ) {
		$classes[] = 'home-page';
	}
	return $classes;
}
add_filter( 'body_class', 'peblog_body_classes' );

// WordPress Customizer
function peblog_customize_register( $wp_customize ) {
	// Home Settings Panel (Main)
	$wp_customize->add_panel(
		'peblog_home_settings',
		array(
			'title'       => __( 'Home Settings', 'peblog' ),
			'description' => __( 'Customize all homepage sections and template parts', 'peblog' ),
			'priority'    => 25,
		)
	);

	// Hero Section (Under Home Settings)
	$wp_customize->add_section(
		'peblog_hero',
		array(
			'title'    => __( 'Hero Section', 'peblog' ),
			'panel'    => 'peblog_home_settings',
			'priority' => 10,
		)
	);

	// Hero Posts Limit
	$wp_customize->add_setting(
		'peblog_hero_posts_limit',
		array(
			'default'           => 6,
			'sanitize_callback' => 'absint',
		)
	);
	$wp_customize->add_control(
		'peblog_hero_posts_limit',
		array(
			'label'       => __( 'Number of Hero Posts', 'peblog' ),
			'description' => __( 'How many posts to display in the hero slider (recommended: 6 for 3D effect)', 'peblog' ),
			'section'     => 'peblog_hero',
			'type'        => 'number',
			'input_attrs' => array(
				'min'  => 1,
				'max'  => 12,
				'step' => 1,
			),
		)
	);

	// Hero Category Selection (Single Select)
	$wp_customize->add_setting(
		'peblog_hero_category',
		array(
			'default'           => '',
			'sanitize_callback' => 'sanitize_text_field',
		)
	);
	$wp_customize->add_control(
		'peblog_hero_category',
		array(
			'label'       => __( 'Hero Category', 'peblog' ),
			'description' => __( 'Select category for hero slider posts (leave empty to show all categories)', 'peblog' ),
			'section'     => 'peblog_hero',
			'type'        => 'select',
			'choices'     => peblog_get_categories_choices(),
		)
	);

	// Hero More Button Text
	$wp_customize->add_setting(
		'peblog_hero_more_text',
		array(
			'default'           => __( 'More', 'peblog' ),
			'sanitize_callback' => 'sanitize_text_field',
		)
	);
	$wp_customize->add_control(
		'peblog_hero_more_text',
		array(
			'label'       => __( 'Hero More Button Text', 'peblog' ),
			'description' => __( 'Text for the "More" button in hero slider', 'peblog' ),
			'section'     => 'peblog_hero',
			'type'        => 'text',
		)
	);

	// Social Media Section
	$wp_customize->add_section(
		'peblog_social',
		array(
			'title'    => __( 'Social Media', 'peblog' ),
			'priority' => 35,
		)
	);

	// Instagram URL
	$wp_customize->add_setting(
		'peblog_instagram_url',
		array(
			'default'           => '',
			'sanitize_callback' => 'esc_url_raw',
		)
	);
	$wp_customize->add_control(
		'peblog_instagram_url',
		array(
			'label'   => __( 'Instagram URL', 'peblog' ),
			'section' => 'peblog_social',
			'type'    => 'url',
		)
	);

	// Twitter URL
	$wp_customize->add_setting(
		'peblog_twitter_url',
		array(
			'default'           => '',
			'sanitize_callback' => 'esc_url_raw',
		)
	);
	$wp_customize->add_control(
		'peblog_twitter_url',
		array(
			'label'   => __( 'Twitter/X URL', 'peblog' ),
			'section' => 'peblog_social',
			'type'    => 'url',
		)
	);

	// LinkedIn URL
	$wp_customize->add_setting(
		'peblog_linkedin_url',
		array(
			'default'           => '',
			'sanitize_callback' => 'esc_url_raw',
		)
	);
	$wp_customize->add_control(
		'peblog_linkedin_url',
		array(
			'label'   => __( 'LinkedIn URL', 'peblog' ),
			'section' => 'peblog_social',
			'type'    => 'url',
		)
	);

	// GitHub URL
	$wp_customize->add_setting(
		'peblog_github_url',
		array(
			'default'           => '',
			'sanitize_callback' => 'esc_url_raw',
		)
	);
	$wp_customize->add_control(
		'peblog_github_url',
		array(
			'label'   => __( 'GitHub URL', 'peblog' ),
			'section' => 'peblog_social',
			'type'    => 'url',
		)
	);

	// Pinterest URL
	$wp_customize->add_setting(
		'peblog_pinterest_url',
		array(
			'default'           => '',
			'sanitize_callback' => 'esc_url_raw',
		)
	);
	$wp_customize->add_control(
		'peblog_pinterest_url',
		array(
			'label'   => __( 'Pinterest URL', 'peblog' ),
			'section' => 'peblog_social',
			'type'    => 'url',
		)
	);

	// Telegram URL
	$wp_customize->add_setting(
		'peblog_telegram_url',
		array(
			'default'           => '',
			'sanitize_callback' => 'esc_url_raw',
		)
	);
	$wp_customize->add_control(
		'peblog_telegram_url',
		array(
			'label'   => __( 'Telegram URL', 'peblog' ),
			'section' => 'peblog_social',
			'type'    => 'url',
		)
	);

	// YouTube URL
	$wp_customize->add_setting(
		'peblog_youtube_url',
		array(
			'default'           => '',
			'sanitize_callback' => 'esc_url_raw',
		)
	);
	$wp_customize->add_control(
		'peblog_youtube_url',
		array(
			'label'   => __( 'YouTube URL', 'peblog' ),
			'section' => 'peblog_social',
			'type'    => 'url',
		)
	);

	// Facebook URL
	$wp_customize->add_setting(
		'peblog_facebook_url',
		array(
			'default'           => '',
			'sanitize_callback' => 'esc_url_raw',
		)
	);
	$wp_customize->add_control(
		'peblog_facebook_url',
		array(
			'label'   => __( 'Facebook URL', 'peblog' ),
			'section' => 'peblog_social',
			'type'    => 'url',
		)
	);

	// Dribbble URL
	$wp_customize->add_setting(
		'peblog_dribbble_url',
		array(
			'default'           => '',
			'sanitize_callback' => 'esc_url_raw',
		)
	);
	$wp_customize->add_control(
		'peblog_dribbble_url',
		array(
			'label'   => __( 'Dribbble URL', 'peblog' ),
			'section' => 'peblog_social',
			'type'    => 'url',
		)
	);

	// Behance URL
	$wp_customize->add_setting(
		'peblog_behance_url',
		array(
			'default'           => '',
			'sanitize_callback' => 'esc_url_raw',
		)
	);
	$wp_customize->add_control(
		'peblog_behance_url',
		array(
			'label'   => __( 'Behance URL', 'peblog' ),
			'section' => 'peblog_social',
			'type'    => 'url',
		)
	);

	// Newsletter Section
	$wp_customize->add_section(
		'peblog_newsletter',
		array(
			'title'    => __( 'Newsletter', 'peblog' ),
			'priority' => 40,
		)
	);

	// Newsletter Title
	$wp_customize->add_setting(
		'peblog_newsletter_title',
		array(
			'default'           => __( 'Join Our Newsletter', 'peblog' ),
			'sanitize_callback' => 'sanitize_text_field',
		)
	);
	$wp_customize->add_control(
		'peblog_newsletter_title',
		array(
			'label'   => __( 'Newsletter Title', 'peblog' ),
			'section' => 'peblog_newsletter',
			'type'    => 'text',
		)
	);

	// Newsletter Form Shortcode
	$wp_customize->add_setting(
		'peblog_newsletter_shortcode',
		array(
			'default'           => '',
			'sanitize_callback' => 'sanitize_text_field',
		)
	);
	$wp_customize->add_control(
		'peblog_newsletter_shortcode',
		array(
			'label'       => __( 'Newsletter Form Shortcode', 'peblog' ),
			'description' => __( 'Enter your newsletter plugin shortcode here. Popular plugins: MailChimp for WordPress, MailPoet, Contact Form 7, etc. Example: [mc4wp_form] or [yikes-mailchimp form="1"]. Leave empty to show a fallback form.', 'peblog' ),
			'section'     => 'peblog_newsletter',
			'type'        => 'text',
		)
	);

	// Featured Posts Section (Under Home Settings)
	$wp_customize->add_section(
		'peblog_featured_posts',
		array(
			'title'    => __( 'Featured Posts', 'peblog' ),
			'panel'    => 'peblog_home_settings',
			'priority' => 20,
		)
	);

	// Featured Posts Title
	$wp_customize->add_setting(
		'peblog_featured_title',
		array(
			'default'           => __( 'Featured Posts', 'peblog' ),
			'sanitize_callback' => 'sanitize_text_field',
		)
	);
	$wp_customize->add_control(
		'peblog_featured_title',
		array(
			'label'   => __( 'Featured Posts Title', 'peblog' ),
			'section' => 'peblog_featured_posts',
			'type'    => 'text',
		)
	);

	// Featured Posts Category
	$wp_customize->add_setting(
		'peblog_featured_category',
		array(
			'default'           => '',
			'sanitize_callback' => 'sanitize_text_field',
		)
	);
	$wp_customize->add_control(
		'peblog_featured_category',
		array(
			'label'   => __( 'Featured Posts Category', 'peblog' ),
			'section' => 'peblog_featured_posts',
			'type'    => 'select',
			'choices' => peblog_get_categories_choices(),
		)
	);

	// Latest Posts Section (Under Home Settings)
	$wp_customize->add_section(
		'peblog_latest_posts',
		array(
			'title'    => __( 'Latest Posts', 'peblog' ),
			'panel'    => 'peblog_home_settings',
			'priority' => 30,
		)
	);

	// Latest Posts Title
	$wp_customize->add_setting(
		'peblog_latest_title',
		array(
			'default'           => __( 'Latest Posts', 'peblog' ),
			'sanitize_callback' => 'sanitize_text_field',
		)
	);
	$wp_customize->add_control(
		'peblog_latest_title',
		array(
			'label'   => __( 'Latest Posts Title', 'peblog' ),
			'section' => 'peblog_latest_posts',
			'type'    => 'text',
		)
	);

	// Latest Posts Category
	$wp_customize->add_setting(
		'peblog_latest_category',
		array(
			'default'           => '',
			'sanitize_callback' => 'sanitize_text_field',
		)
	);
	$wp_customize->add_control(
		'peblog_latest_category',
		array(
			'label'   => __( 'Latest Posts Category', 'peblog' ),
			'section' => 'peblog_latest_posts',
			'type'    => 'select',
			'choices' => peblog_get_categories_choices(),
		)
	);

	// Most Read Section (Under Home Settings)
	$wp_customize->add_section(
		'peblog_most_read',
		array(
			'title'    => __( 'Most Read', 'peblog' ),
			'panel'    => 'peblog_home_settings',
			'priority' => 35,
		)
	);

	// Most Read Post Selection
	$wp_customize->add_setting(
		'peblog_most_read_post',
		array(
			'default'           => '',
			'sanitize_callback' => 'absint',
		)
	);
	$wp_customize->add_control(
		'peblog_most_read_post',
		array(
			'label'       => __( 'Select Specific Post', 'peblog' ),
			'description' => __( 'Choose a specific post to display in Most Read section. If empty, the most viewed post will be shown automatically.', 'peblog' ),
			'section'     => 'peblog_most_read',
			'type'        => 'select',
			'choices'     => peblog_get_posts_choices(),
		)
	);

	// Show Popular Tags in Most Read Section
	$wp_customize->add_setting(
		'peblog_most_read_show_tags',
		array(
			'default'           => true,
			'sanitize_callback' => 'wp_validate_boolean',
		)
	);
	$wp_customize->add_control(
		'peblog_most_read_show_tags',
		array(
			'label'       => __( 'Show Popular Tags', 'peblog' ),
			'description' => __( 'Display popular tags section in Most Read area', 'peblog' ),
			'section'     => 'peblog_most_read',
			'type'        => 'checkbox',
		)
	);

	// Author Section
	$wp_customize->add_section(
		'peblog_author',
		array(
			'title'    => __( 'Author Information (Most Read)', 'peblog' ),
			'panel'    => 'peblog_home_settings',
			'priority' => 40,
		)
	);

	// Author Name
	$wp_customize->add_setting(
		'peblog_author_name',
		array(
			'default'           => __( 'John Doe', 'peblog' ),
			'sanitize_callback' => 'sanitize_text_field',
		)
	);
	$wp_customize->add_control(
		'peblog_author_name',
		array(
			'label'   => __( 'Author Name', 'peblog' ),
			'section' => 'peblog_author',
			'type'    => 'text',
		)
	);

	// Author Title
	$wp_customize->add_setting(
		'peblog_author_title',
		array(
			'default'           => __( 'Blog Author', 'peblog' ),
			'sanitize_callback' => 'sanitize_text_field',
		)
	);
	$wp_customize->add_control(
		'peblog_author_title',
		array(
			'label'   => __( 'Author Title', 'peblog' ),
			'section' => 'peblog_author',
			'type'    => 'text',
		)
	);

	// Author Description
	$wp_customize->add_setting(
		'peblog_author_description',
		array(
			'default'           => __( 'Passionate about technology, design, and innovation.', 'peblog' ),
			'sanitize_callback' => 'sanitize_textarea_field',
		)
	);
	$wp_customize->add_control(
		'peblog_author_description',
		array(
			'label'   => __( 'Author Description', 'peblog' ),
			'section' => 'peblog_author',
			'type'    => 'textarea',
		)
	);

	// Author Image
	$wp_customize->add_setting(
		'peblog_author_image',
		array(
			'default'           => '',
			'sanitize_callback' => 'esc_url_raw',
		)
	);
	$wp_customize->add_control(
		new WP_Customize_Image_Control(
			$wp_customize,
			'peblog_author_image',
			array(
				'label'   => __( 'Author Image', 'peblog' ),
				'section' => 'peblog_author',
			)
		)
	);

	// Logo & Branding Section (Theme-specific logos - WordPress Site Identity takes priority)
	$wp_customize->add_section(
		'peblog_logo',
		array(
			'title'       => __( 'Theme Logo Options', 'peblog' ),
			'description' => __( '<strong>Note:</strong> WordPress Site Identity (Appearance > Customize > Site Identity) is the primary logo setting. These options are fallback logos if Site Identity logo is not set, or additional dark mode logo support.', 'peblog' ),
			'priority'    => 20,
		)
	);

	// Site Logo (Fallback - only used if WordPress Site Identity logo is not set)
	$wp_customize->add_setting(
		'peblog_logo',
		array(
			'default'           => '',
			'sanitize_callback' => 'esc_url_raw',
		)
	);
	$wp_customize->add_control(
		new WP_Customize_Image_Control(
			$wp_customize,
			'peblog_logo',
			array(
				'label'       => __( 'Fallback Site Logo', 'peblog' ),
				'description' => __( 'Upload your site logo (light mode). This is used only if no logo is set in Site Identity. Recommended size: 200x60px', 'peblog' ),
				'section'     => 'peblog_logo',
			)
		)
	);

	// Site Logo (White/Dark version - for dark mode support)
	$wp_customize->add_setting(
		'peblog_logo_white',
		array(
			'default'           => '',
			'sanitize_callback' => 'esc_url_raw',
		)
	);
	$wp_customize->add_control(
		new WP_Customize_Image_Control(
			$wp_customize,
			'peblog_logo_white',
			array(
				'label'       => __( 'Dark Mode Logo', 'peblog' ),
				'description' => __( 'Upload white/dark version of your logo for dark mode. Works with both WordPress Site Identity logo and theme fallback logo. Recommended size: 200x60px', 'peblog' ),
				'section'     => 'peblog_logo',
			)
		)
	);

	// Favicon and Apple Touch Icon are now handled automatically by WordPress Site Icon setting in Customizer (WordPress 4.3+)

	// Header Section
	$wp_customize->add_section(
		'peblog_header',
		array(
			'title'    => __( 'Header Settings', 'peblog' ),
			'priority' => 25,
		)
	);

	// Action Button Text
	$wp_customize->add_setting(
		'peblog_action_button_text',
		array(
			'default'           => __( 'Subscribe', 'peblog' ),
			'sanitize_callback' => 'sanitize_text_field',
		)
	);
	$wp_customize->add_control(
		'peblog_action_button_text',
		array(
			'label'   => __( 'Action Button Text', 'peblog' ),
			'section' => 'peblog_header',
			'type'    => 'text',
		)
	);

	// Action Button URL
	$wp_customize->add_setting(
		'peblog_action_button_url',
		array(
			'default'           => '#',
			'sanitize_callback' => 'esc_url_raw',
		)
	);
	$wp_customize->add_control(
		'peblog_action_button_url',
		array(
			'label'   => __( 'Action Button URL', 'peblog' ),
			'section' => 'peblog_header',
			'type'    => 'url',
		)
	);

	// Action Button Target
	$wp_customize->add_setting(
		'peblog_action_button_target',
		array(
			'default'           => '_self',
			'sanitize_callback' => 'sanitize_text_field',
		)
	);
	$wp_customize->add_control(
		'peblog_action_button_target',
		array(
			'label'   => __( 'Action Button Target', 'peblog' ),
			'section' => 'peblog_header',
			'type'    => 'select',
			'choices' => array(
				'_self'  => __( 'Same Window', 'peblog' ),
				'_blank' => __( 'New Window', 'peblog' ),
			),
		)
	);

	// Single Page Section (Separate from Home Settings)
	$wp_customize->add_section(
		'peblog_single_page',
		array(
			'title'       => __( 'Single Page Settings', 'peblog' ),
			'description' => __( 'Customize single post page settings, author card and sidebar', 'peblog' ),
			'priority'    => 55,
		)
	);

	// Show Author Card on Single Page
	$wp_customize->add_setting(
		'peblog_show_author_card',
		array(
			'default'           => true,
			'sanitize_callback' => 'wp_validate_boolean',
		)
	);
	$wp_customize->add_control(
		'peblog_show_author_card',
		array(
			'label'       => __( 'Show Author Card', 'peblog' ),
			'description' => __( 'Display author card below post content on single pages', 'peblog' ),
			'section'     => 'peblog_single_page',
			'type'        => 'checkbox',
		)
	);

	// Single Page Author Card uses Customizer Author Info
	// (Uses the same settings from peblog_author section)

	// Sidebar Position (Inside Single Page Settings)
	$wp_customize->add_setting(
		'peblog_sidebar_position',
		array(
			'default'           => 'none',
			'sanitize_callback' => 'sanitize_text_field',
		)
	);
	$wp_customize->add_control(
		'peblog_sidebar_position',
		array(
			'label'       => __( 'Sidebar Position', 'peblog' ),
			'description' => __( 'Choose where to display the sidebar', 'peblog' ),
			'section'     => 'peblog_single_page',
			'type'        => 'select',
			'choices'     => array(
				'none'  => __( 'No Sidebar', 'peblog' ),
				'left'  => __( 'Left Side', 'peblog' ),
				'right' => __( 'Right Side', 'peblog' ),
			),
		)
	);

	// Show Sidebar on Single Posts (Inside Single Page Settings)
	$wp_customize->add_setting(
		'peblog_sidebar_single',
		array(
			'default'           => false,
			'sanitize_callback' => 'wp_validate_boolean',
		)
	);
	$wp_customize->add_control(
		'peblog_sidebar_single',
		array(
			'label'       => __( 'Show Sidebar on Single Posts', 'peblog' ),
			'description' => __( 'Display sidebar on single post pages', 'peblog' ),
			'section'     => 'peblog_single_page',
			'type'        => 'checkbox',
		)
	);

	// Show Sidebar on Pages (Inside Single Page Settings)
	$wp_customize->add_setting(
		'peblog_sidebar_page',
		array(
			'default'           => false,
			'sanitize_callback' => 'wp_validate_boolean',
		)
	);
	$wp_customize->add_control(
		'peblog_sidebar_page',
		array(
			'label'       => __( 'Show Sidebar on Pages', 'peblog' ),
			'description' => __( 'Display sidebar on static pages', 'peblog' ),
			'section'     => 'peblog_single_page',
			'type'        => 'checkbox',
		)
	);

	// Show Post Tags
	$wp_customize->add_setting(
		'peblog_show_post_tags',
		array(
			'default'           => true,
			'sanitize_callback' => 'wp_validate_boolean',
		)
	);
	$wp_customize->add_control(
		'peblog_show_post_tags',
		array(
			'label'       => __( 'Show Post Tags', 'peblog' ),
			'description' => __( 'Display post tags below post content on single pages', 'peblog' ),
			'section'     => 'peblog_single_page',
			'type'        => 'checkbox',
		)
	);

	// Social Share Buttons Section
	$wp_customize->add_setting(
		'peblog_social_share_title',
		array(
			'default'           => __( 'Share this post', 'peblog' ),
			'sanitize_callback' => 'sanitize_text_field',
		)
	);
	$wp_customize->add_control(
		'peblog_social_share_title',
		array(
			'label'       => __( 'Social Share Title', 'peblog' ),
			'description' => __( 'Title text above social share buttons', 'peblog' ),
			'section'     => 'peblog_single_page',
			'type'        => 'text',
		)
	);

	// Facebook Share
	$wp_customize->add_setting(
		'peblog_share_facebook',
		array(
			'default'           => true,
			'sanitize_callback' => 'wp_validate_boolean',
		)
	);
	$wp_customize->add_control(
		'peblog_share_facebook',
		array(
			'label'   => __( 'Show Facebook Share Button', 'peblog' ),
			'section' => 'peblog_single_page',
			'type'    => 'checkbox',
		)
	);

	// Twitter/X Share
	$wp_customize->add_setting(
		'peblog_share_twitter',
		array(
			'default'           => true,
			'sanitize_callback' => 'wp_validate_boolean',
		)
	);
	$wp_customize->add_control(
		'peblog_share_twitter',
		array(
			'label'   => __( 'Show Twitter/X Share Button', 'peblog' ),
			'section' => 'peblog_single_page',
			'type'    => 'checkbox',
		)
	);

	// LinkedIn Share
	$wp_customize->add_setting(
		'peblog_share_linkedin',
		array(
			'default'           => true,
			'sanitize_callback' => 'wp_validate_boolean',
		)
	);
	$wp_customize->add_control(
		'peblog_share_linkedin',
		array(
			'label'   => __( 'Show LinkedIn Share Button', 'peblog' ),
			'section' => 'peblog_single_page',
			'type'    => 'checkbox',
		)
	);

	// WhatsApp Share
	$wp_customize->add_setting(
		'peblog_share_whatsapp',
		array(
			'default'           => true,
			'sanitize_callback' => 'wp_validate_boolean',
		)
	);
	$wp_customize->add_control(
		'peblog_share_whatsapp',
		array(
			'label'   => __( 'Show WhatsApp Share Button', 'peblog' ),
			'section' => 'peblog_single_page',
			'type'    => 'checkbox',
		)
	);

	// Telegram Share
	$wp_customize->add_setting(
		'peblog_share_telegram',
		array(
			'default'           => false,
			'sanitize_callback' => 'wp_validate_boolean',
		)
	);
	$wp_customize->add_control(
		'peblog_share_telegram',
		array(
			'label'   => __( 'Show Telegram Share Button', 'peblog' ),
			'section' => 'peblog_single_page',
			'type'    => 'checkbox',
		)
	);

	// Pinterest Share
	$wp_customize->add_setting(
		'peblog_share_pinterest',
		array(
			'default'           => false,
			'sanitize_callback' => 'wp_validate_boolean',
		)
	);
	$wp_customize->add_control(
		'peblog_share_pinterest',
		array(
			'label'   => __( 'Show Pinterest Share Button', 'peblog' ),
			'section' => 'peblog_single_page',
			'type'    => 'checkbox',
		)
	);

	// Category Icons Section
	$wp_customize->add_section(
		'peblog_category_icons',
		array(
			'title'    => __( 'Category Icons', 'peblog' ),
			'priority' => 60,
		)
	);

	// All Categories Section (Under Home Settings)
	$wp_customize->add_section(
		'peblog_all_categories',
		array(
			'title'    => __( 'All Categories Section', 'peblog' ),
			'panel'    => 'peblog_home_settings',
			'priority' => 50,
		)
	);

	// All Categories Title
	$wp_customize->add_setting(
		'peblog_all_categories_title',
		array(
			'default'           => __( 'Explore Categories', 'peblog' ),
			'sanitize_callback' => 'sanitize_text_field',
		)
	);
	$wp_customize->add_control(
		'peblog_all_categories_title',
		array(
			'label'       => __( 'Section Title', 'peblog' ),
			/* translators: %s: Default section title */
			'description' => sprintf( __( 'Change the title of the category filter section. Default: "%s"', 'peblog' ), __( 'Explore Categories', 'peblog' ) ),
			'section'     => 'peblog_all_categories',
			'type'        => 'text',
		)
	);

	// Number of Posts to Show
	$wp_customize->add_setting(
		'peblog_category_posts_limit',
		array(
			'default'           => 12,
			'sanitize_callback' => 'absint',
		)
	);
	$wp_customize->add_control(
		'peblog_category_posts_limit',
		array(
			'label'       => __( 'Number of Posts to Show', 'peblog' ),
			'description' => __( 'How many posts to display in the category filter section', 'peblog' ),
			'section'     => 'peblog_all_categories',
			'type'        => 'number',
			'input_attrs' => array(
				'min'  => 1,
				'max'  => 50,
				'step' => 1,
			),
		)
	);

	// Show Empty Categories
	$wp_customize->add_setting(
		'peblog_show_empty_categories',
		array(
			'default'           => false,
			'sanitize_callback' => 'wp_validate_boolean',
		)
	);
	$wp_customize->add_control(
		'peblog_show_empty_categories',
		array(
			'label'       => __( 'Show Empty Categories', 'peblog' ),
			'description' => __( 'Show categories that have no posts', 'peblog' ),
			'section'     => 'peblog_all_categories',
			'type'        => 'checkbox',
		)
	);

	// Selected Category (Single Select)
	$wp_customize->add_setting(
		'peblog_selected_category',
		array(
			'default'           => '',
			'sanitize_callback' => 'peblog_sanitize_category_slugs',
		)
	);
	$wp_customize->add_control(
		'peblog_selected_category',
		array(
			'label'       => __( 'Filter by Category', 'peblog' ),
			'description' => __( 'Enter category slugs separated by commas (leave empty to show all categories)', 'peblog' ),
			'section'     => 'peblog_all_categories',
			'type'        => 'text',
			'input_attrs' => array(
				'placeholder' => __( 'e.g. design,software,news', 'peblog' ),
			),
		)
	);

	// Get all categories and add icon settings for each
	$categories = get_categories( array( 'hide_empty' => false ) );
	foreach ( $categories as $category ) {
		$wp_customize->add_setting(
			'peblog_category_icon_' . $category->slug,
			array(
				'default'           => '',
				'sanitize_callback' => 'esc_url_raw',
			)
		);
		$wp_customize->add_control(
			new WP_Customize_Image_Control(
				$wp_customize,
				'peblog_category_icon_' . $category->slug,
				array(
					/* translators: %s: Category name */
					'label'   => sprintf( __( '%s Icon', 'peblog' ), $category->name ),
					'section' => 'peblog_category_icons',
				)
			)
		);
	}
}
add_action( 'customize_register', 'peblog_customize_register' );

// Get posts for customizer choices
function peblog_get_posts_choices() {
	$choices = array( '' => __( 'Default (Most Viewed)', 'peblog' ) );
	$posts   = get_posts(
		array(
			'numberposts' => -1,
			'post_status' => 'publish',
			'orderby'     => 'date',
			'order'       => 'DESC',
		)
	);

	foreach ( $posts as $post ) {
		$choices[ absint( $post->ID ) ] = esc_html( $post->post_title ) . ' (' . __( 'ID', 'peblog' ) . ': ' . absint( $post->ID ) . ')';
	}

	return $choices;
}

// Get categories for customizer choices
function peblog_get_categories_choices() {
	$categories = get_categories( array( 'hide_empty' => false ) );
	$choices    = array( '' => __( 'All Categories', 'peblog' ) );

	foreach ( $categories as $category ) {
		$choices[ $category->slug ] = $category->name;
	}

	return $choices;
}

/**
 * Sanitize category slugs input.
 *
 * @param string|array $value Comma-separated category slugs or array.
 * @return string Sanitized category slugs.
 */
function peblog_sanitize_category_slugs( $value ) {
	if ( is_array( $value ) ) {
		$value = implode( ',', $value );
	}

	$slugs = array_filter( array_map( 'trim', explode( ',', (string) $value ) ) );

	$sanitized = array();
	foreach ( $slugs as $slug ) {
		$sanitized_slug = sanitize_title( $slug );
		if ( ! empty( $sanitized_slug ) ) {
			$sanitized[] = $sanitized_slug;
		}
	}

	return implode( ',', array_unique( $sanitized ) );
}

// Admin Options Page
function peblog_admin_menu() {
	add_theme_page(
		__( 'Peblog Options', 'peblog' ),
		__( 'Peblog Options', 'peblog' ),
		'edit_theme_options',
		'peblog-options',
		'peblog_admin_page'
	);
}
add_action( 'admin_menu', 'peblog_admin_menu' );

/**
 * Enqueue admin styles.
 *
 * @param string $hook Current admin page hook.
 */
function peblog_enqueue_admin_styles( $hook ) {
	// Only load on our admin page.
	if ( 'appearance_page_peblog-options' !== $hook ) {
		return;
	}
	wp_enqueue_style( 'peblog-admin-style', get_template_directory_uri() . '/assets/css/admin.css', array(), '1.0.0' );
}
add_action( 'admin_enqueue_scripts', 'peblog_enqueue_admin_styles' );

/**
 * Render admin options page.
 */
function peblog_admin_page() {
	$customize_url = admin_url( 'customize.php' );
	$theme_url     = 'https://github.com/emrahyldz93/peblog';
	?>
	<div class="wrap">
		<h1><?php esc_html_e( 'Peblog Options', 'peblog' ); ?></h1>
		
		<div class="peblog-admin-content">
			<div class="peblog-admin-section">
				<h2><?php esc_html_e( 'Theme Information', 'peblog' ); ?></h2>
				<table class="form-table">
					<tr>
						<th><?php esc_html_e( 'Version', 'peblog' ); ?></th>
						<td><?php echo esc_html( wp_get_theme()->get( 'Version' ) ); ?></td>
					</tr>
					<tr>
						<th><?php esc_html_e( 'Author', 'peblog' ); ?></th>
						<td><?php echo esc_html( wp_get_theme()->get( 'Author' ) ); ?></td>
					</tr>
					<tr>
						<th><?php esc_html_e( 'License', 'peblog' ); ?></th>
						<td><?php echo esc_html( wp_get_theme()->get( 'License' ) ); ?></td>
					</tr>
					<tr>
						<th><?php esc_html_e( 'Requires', 'peblog' ); ?></th>
						<td>
						<?php
						/* translators: 1: WordPress version, 2: PHP version */
						printf( esc_html__( 'WordPress %1$s+, PHP %2$s+', 'peblog' ), esc_html( wp_get_theme()->get( 'RequiresWP' ) ), esc_html( wp_get_theme()->get( 'RequiresPHP' ) ) );
						?>
						</td>
					</tr>
				</table>
				<p>
					<a href="<?php echo esc_url( $theme_url ); ?>" target="_blank" class="button button-secondary">
						<?php esc_html_e( 'View on GitHub', 'peblog' ); ?>
					</a>
					<a href="<?php echo esc_url( $customize_url ); ?>" class="button button-primary">
						<?php esc_html_e( 'Customize Theme', 'peblog' ); ?>
					</a>
				</p>
			</div>
			
			<div class="peblog-admin-section">
				<h2><?php esc_html_e( 'Documentation', 'peblog' ); ?></h2>
				<div class="peblog-docs">
					<h3><?php esc_html_e( 'Quick Start', 'peblog' ); ?></h3>
					<ol>
						<li><?php esc_html_e( 'Go to Appearance > Customize to configure your theme', 'peblog' ); ?></li>
						<li><?php esc_html_e( 'Create a menu in Appearance > Menus', 'peblog' ); ?></li>
						<li><?php esc_html_e( 'Add blog posts with categories and tags', 'peblog' ); ?></li>
						<li><?php esc_html_e( 'Upload your logo in Appearance > Customize > Site Identity', 'peblog' ); ?></li>
					</ol>
					
					<h3><?php esc_html_e( 'Theme Settings', 'peblog' ); ?></h3>
					<p><?php esc_html_e( 'All theme settings are located in Appearance > Customize:', 'peblog' ); ?></p>
					<ul>
						<li><strong><?php esc_html_e( 'Home Settings', 'peblog' ); ?></strong>: <?php esc_html_e( 'Hero Slider, Featured Posts, Latest Posts, Most Read, Explore Categories', 'peblog' ); ?></li>
						<li><strong><?php esc_html_e( 'Header Settings', 'peblog' ); ?></strong>: <?php esc_html_e( 'Logo, Action Button', 'peblog' ); ?></li>
						<li><strong><?php esc_html_e( 'Single Page Settings', 'peblog' ); ?></strong>: <?php esc_html_e( 'Author Card, Sidebar, Post Tags, Social Share', 'peblog' ); ?></li>
						<li><strong><?php esc_html_e( 'Social Media', 'peblog' ); ?></strong>: <?php esc_html_e( 'Add your social media URLs', 'peblog' ); ?></li>
						<li><strong><?php esc_html_e( 'Newsletter', 'peblog' ); ?></strong>: <?php esc_html_e( 'Add plugin shortcode (MailChimp, MailPoet, etc.)', 'peblog' ); ?></li>
					</ul>
					
				<h3><?php esc_html_e( 'Features', 'peblog' ); ?></h3>
				<ul>
					<li><?php esc_html_e( 'Dark/Light Mode with localStorage', 'peblog' ); ?></li>
					<li><?php esc_html_e( 'Fully responsive design', 'peblog' ); ?></li>
					<li><?php esc_html_e( 'Swiper sliders for hero and latest posts', 'peblog' ); ?></li>
					<li><?php esc_html_e( 'Post views counter support (via plugin)', 'peblog' ); ?></li>
					<li><?php esc_html_e( 'Social sharing buttons', 'peblog' ); ?></li>
					<li><?php esc_html_e( 'Newsletter integration', 'peblog' ); ?></li>
					<li><?php esc_html_e( 'Archive templates (category, tag, author, search)', 'peblog' ); ?></li>
				</ul>
				
				<h3><?php esc_html_e( 'Recommended Plugins', 'peblog' ); ?></h3>
				<p><?php esc_html_e( 'These plugins work great with Peblog theme:', 'peblog' ); ?></p>
				<ul>
					<li>
						<strong><?php esc_html_e( 'Post Views Counter', 'peblog' ); ?></strong>
						<?php if ( function_exists( 'pvc_get_post_views' ) ) : ?>
							<span class="dashicons dashicons-yes-alt" style="color: #46b450;"></span>
							<span style="color: #46b450;"><?php esc_html_e( 'Active', 'peblog' ); ?></span>
						<?php else : ?>
							<span class="dashicons dashicons-marker" style="color: #f56e28;"></span>
							<a href="<?php echo esc_url( admin_url( 'plugin-install.php?s=post+views+counter&tab=search&type=term' ) ); ?>"><?php esc_html_e( 'Install Plugin', 'peblog' ); ?></a>
						<?php endif; ?>
						<br>
						<small><?php esc_html_e( 'Display post view counts throughout your site. Theme has built-in support for this plugin.', 'peblog' ); ?></small>
					</li>
				</ul>
				</div>
			</div>
		</div>
	</div>
	<?php
}

