<?php
/**
 * FansPaysite Customizer settings.
 *
 * Organized into a panel with sections: Layout, Header, Colors,
 * Typography, Post Display, Footer.
 *
 * @package FansPaysite
 * @since   2.2
 */

function fanspaysite_customize_register( $wp_customize ) {

	// ===== Panel =====
	$wp_customize->add_panel( 'fanspaysite_panel', array(
		'title'    => __( 'FansPaysite Theme', 'fanspaysite' ),
		'priority' => 160,
	) );

	// ===== Sections =====
	$wp_customize->add_section( 'fanspaysite_layout_section', array(
		'title'    => __( 'Layout', 'fanspaysite' ),
		'panel'    => 'fanspaysite_panel',
		'priority' => 10,
	) );

	$wp_customize->add_section( 'fanspaysite_header_section', array(
		'title'    => __( 'Header & Menu', 'fanspaysite' ),
		'panel'    => 'fanspaysite_panel',
		'priority' => 20,
	) );

	$wp_customize->add_section( 'fanspaysite_colors_section', array(
		'title'    => __( 'Colors', 'fanspaysite' ),
		'panel'    => 'fanspaysite_panel',
		'priority' => 30,
	) );

	$wp_customize->add_section( 'fanspaysite_typography_section', array(
		'title'    => __( 'Typography', 'fanspaysite' ),
		'panel'    => 'fanspaysite_panel',
		'priority' => 40,
	) );

	$wp_customize->add_section( 'fanspaysite_post_display_section', array(
		'title'    => __( 'Post Display', 'fanspaysite' ),
		'panel'    => 'fanspaysite_panel',
		'priority' => 50,
	) );

	$wp_customize->add_section( 'fanspaysite_footer_section', array(
		'title'    => __( 'Footer', 'fanspaysite' ),
		'panel'    => 'fanspaysite_panel',
		'priority' => 60,
	) );

	// =====================================================
	// Title & Tagline (WP default section)
	// =====================================================
	$wp_customize->add_setting( 'display_site_title', array(
		'default'           => true,
		'transport'         => 'refresh',
		'sanitize_callback' => 'fanspaysite_sanitize_checkbox',
	) );
	$wp_customize->add_control( 'display_site_title', array(
		'label'   => __( 'Display Site Title', 'fanspaysite' ),
		'section' => 'title_tagline',
		'type'    => 'checkbox',
	) );

	$wp_customize->add_setting( 'display_site_tagline', array(
		'default'           => true,
		'transport'         => 'refresh',
		'sanitize_callback' => 'fanspaysite_sanitize_checkbox',
	) );
	$wp_customize->add_control( 'display_site_tagline', array(
		'label'   => __( 'Display Site Tagline', 'fanspaysite' ),
		'section' => 'title_tagline',
		'type'    => 'checkbox',
	) );

	// =====================================================
	// Layout Section
	// =====================================================
	$wp_customize->add_setting( 'fanspaysite_layout', array(
		'default'           => 'left-sidebar',
		'sanitize_callback' => 'fanspaysite_sanitize_layout',
		'transport'         => 'refresh',
	) );
	$wp_customize->add_control( 'fanspaysite_layout', array(
		'label'       => __( 'Menu Layout', 'fanspaysite' ),
		'section'     => 'fanspaysite_layout_section',
		'type'        => 'radio',
		'choices'     => array(
			'left-sidebar' => __( 'Left Sidebar', 'fanspaysite' ),
			'top-bar'      => __( 'Top Bar', 'fanspaysite' ),
		),
		'description' => __( 'Choose the menu placement.', 'fanspaysite' ),
		'priority'    => 1,
	) );

	// Content width
	$wp_customize->add_setting( 'fanspaysite_content_width', array(
		'default'           => 'default',
		'sanitize_callback' => 'fanspaysite_sanitize_content_width',
	) );
	$wp_customize->add_control( 'fanspaysite_content_width', array(
		'label'   => __( 'Content Width', 'fanspaysite' ),
		'section' => 'fanspaysite_layout_section',
		'type'    => 'radio',
		'choices' => array(
			'default' => __( 'Default', 'fanspaysite' ),
			'wide'    => __( 'Wide', 'fanspaysite' ),
			'boxed'   => __( 'Boxed', 'fanspaysite' ),
		),
		'priority' => 2,
	) );

	// Secondary sidebar toggle
	$wp_customize->add_setting( 'fanspaysite_show_right_sidebar', array(
		'default'           => 'auto',
		'sanitize_callback' => 'fanspaysite_sanitize_right_sidebar',
	) );
	$wp_customize->add_control( 'fanspaysite_show_right_sidebar', array(
		'label'       => __( 'Right Sidebar', 'fanspaysite' ),
		'section'     => 'fanspaysite_layout_section',
		'type'        => 'radio',
		'choices'     => array(
			'auto' => __( 'Auto (visible on wide screens)', 'fanspaysite' ),
			'yes'  => __( 'Always visible', 'fanspaysite' ),
			'no'   => __( 'Hidden', 'fanspaysite' ),
		),
		'priority' => 3,
	) );

	// Sticky Menu Bar (Layout section)
	$wp_customize->add_setting( 'fanspaysite_sticky_header', array(
		'default'           => true,
		'sanitize_callback' => 'fanspaysite_sanitize_checkbox',
	) );
	$wp_customize->add_control( 'fanspaysite_sticky_header', array(
		'label'       => __( 'Sticky Menu Bar', 'fanspaysite' ),
		'section'     => 'fanspaysite_layout_section',
		'type'        => 'checkbox',
		'description' => __( 'Keep the menu bar fixed while scrolling. When disabled, the page scrolls as a whole.', 'fanspaysite' ),
		'priority'    => 4,
	) );

	// =====================================================
	// Header & Menu Section
	// =====================================================

	// Burger Menu
	$wp_customize->add_setting( 'fanspaysite_burger_menu', array(
		'default'           => 'auto',
		'sanitize_callback' => 'fanspaysite_sanitize_burger_menu',
		'transport'         => 'refresh',
	) );
	$wp_customize->add_control( 'fanspaysite_burger_menu', array(
		'label'       => __( 'Burger Menu', 'fanspaysite' ),
		'section'     => 'fanspaysite_header_section',
		'type'        => 'radio',
		'choices'     => array(
			'yes'  => __( 'Yes (always show burger button)', 'fanspaysite' ),
			'no'   => __( 'No (full menu always visible)', 'fanspaysite' ),
			'auto' => __( 'Auto (burger on narrow/touch screens)', 'fanspaysite' ),
		),
		'description' => __( 'Controls the burger menu button for both layouts.', 'fanspaysite' ),
		'priority'    => 2,
	) );

	// Main Menu Display Mode
	$wp_customize->add_setting( 'fanspaysite_menu_display_mode', array(
		'default'           => 'auto',
		'sanitize_callback' => 'fanspaysite_sanitize_menu_display_mode',
	) );
	$wp_customize->add_control( 'fanspaysite_menu_display_mode', array(
		'label'       => __( 'Main Menu Display', 'fanspaysite' ),
		'section'     => 'fanspaysite_header_section',
		'type'        => 'radio',
		'choices'     => array(
			'big_icon'        => __( 'Big Icon No Text', 'fanspaysite' ),
			'small_icon_text' => __( 'Small Icon + Text', 'fanspaysite' ),
			'auto'            => __( 'Auto (big icons on narrow/touch)', 'fanspaysite' ),
			'auto_hide'       => __( 'Auto Hide (hide menu when burger active)', 'fanspaysite' ),
		),
		'priority' => 5,
	) );

	// Secondary Menus Display Mode
	$wp_customize->add_setting( 'fanspaysite_submenu_display_mode', array(
		'default'           => 'auto',
		'sanitize_callback' => 'fanspaysite_sanitize_submenu_display_mode',
	) );
	$wp_customize->add_control( 'fanspaysite_submenu_display_mode', array(
		'label'       => __( 'Secondary Menus', 'fanspaysite' ),
		'section'     => 'fanspaysite_header_section',
		'type'        => 'radio',
		'choices'     => array(
			'big_icon'        => __( 'Big Icons No Text', 'fanspaysite' ),
			'small_icon_text' => __( 'Small Icon + Text', 'fanspaysite' ),
			'burger'          => __( 'Burger (full-page, click to open)', 'fanspaysite' ),
			'auto'            => __( 'Auto (icons & text, burger on narrow)', 'fanspaysite' ),
		),
		'priority' => 6,
	) );

	// Submenu Hover
	$wp_customize->add_setting( 'fanspaysite_submenu_hover', array(
		'default'           => 'auto',
		'sanitize_callback' => 'fanspaysite_sanitize_submenu_hover',
	) );
	$wp_customize->add_control( 'fanspaysite_submenu_hover', array(
		'label'   => __( 'Open Submenus on Hover', 'fanspaysite' ),
		'section' => 'fanspaysite_header_section',
		'type'    => 'radio',
		'choices' => array(
			'yes'  => __( 'Yes', 'fanspaysite' ),
			'no'   => __( 'No (click to open)', 'fanspaysite' ),
			'auto' => __( 'Auto (hover on desktop, click on touch)', 'fanspaysite' ),
		),
		'priority' => 7,
	) );

	// Default theme mode
	$wp_customize->add_setting( 'fanspaysite_default_theme_mode', array(
		'default'           => 'light',
		'sanitize_callback' => 'fanspaysite_sanitize_theme_mode',
	) );
	$wp_customize->add_control( 'fanspaysite_default_theme_mode', array(
		'label'       => __( 'Default Theme Mode', 'fanspaysite' ),
		'section'     => 'fanspaysite_header_section',
		'type'        => 'radio',
		'choices'     => array(
			'light' => __( 'Light', 'fanspaysite' ),
			'dark'  => __( 'Dark', 'fanspaysite' ),
			'auto'  => __( 'Auto (follows system preference)', 'fanspaysite' ),
		),
		'priority' => 10,
	) );

	// Show mode switcher
	$wp_customize->add_setting( 'fanspaysite_show_theme_mode_switcher', array(
		'default'           => 'yes',
		'sanitize_callback' => 'fanspaysite_sanitize_show_switcher',
	) );
	$wp_customize->add_control( 'fanspaysite_show_theme_mode_switcher', array(
		'label'   => __( 'Show Frontend Mode Switcher', 'fanspaysite' ),
		'section' => 'fanspaysite_header_section',
		'type'    => 'checkbox',
		'priority' => 11,
	) );

	// =====================================================
	// Colors Section
	// =====================================================
	$colors = array(
		'light_mode_background_color' => array(
			'default' => '#ffffff',
			'label'   => __( 'Background (Light Mode)', 'fanspaysite' ),
		),
		'light_mode_text_color' => array(
			'default' => '#333333',
			'label'   => __( 'Text (Light Mode)', 'fanspaysite' ),
		),
		'light_mode_link_color' => array(
			'default' => '#77145d',
			'label'   => __( 'Links (Light Mode)', 'fanspaysite' ),
		),
		'dark_mode_background_color' => array(
			'default' => '#333333',
			'label'   => __( 'Background (Dark Mode)', 'fanspaysite' ),
		),
		'dark_mode_text_color' => array(
			'default' => '#ffffff',
			'label'   => __( 'Text (Dark Mode)', 'fanspaysite' ),
		),
		'dark_mode_link_color' => array(
			'default' => '#d39ed8',
			'label'   => __( 'Links (Dark Mode)', 'fanspaysite' ),
		),
	);

	foreach ( $colors as $key => $color ) {
		$wp_customize->add_setting( $key, array(
			'default'           => $color['default'],
			'sanitize_callback' => 'sanitize_hex_color',
		) );
		$wp_customize->add_control( new WP_Customize_Color_Control( $wp_customize, $key, array(
			'label'   => $color['label'],
			'section' => 'fanspaysite_colors_section',
		) ) );
	}

	// =====================================================
	// Typography Section
	// =====================================================
	$wp_customize->add_setting( 'fanspaysite_font_preset', array(
		'default'           => 'system',
		'sanitize_callback' => 'fanspaysite_sanitize_font_preset',
	) );
	$wp_customize->add_control( 'fanspaysite_font_preset', array(
		'label'   => __( 'Font Style', 'fanspaysite' ),
		'section' => 'fanspaysite_typography_section',
		'type'    => 'select',
		'choices' => array(
			'system'  => __( 'System Default', 'fanspaysite' ),
			'serif'   => __( 'Serif (Georgia)', 'fanspaysite' ),
			'modern'  => __( 'Modern (Inter)', 'fanspaysite' ),
			'rounded' => __( 'Rounded (Nunito)', 'fanspaysite' ),
		),
	) );

	// =====================================================
	// Post Display Section
	// =====================================================

	// Archive Display Mode
	$wp_customize->add_setting( 'fanspaysite_archive_display', array(
		'default'           => 'boxes',
		'sanitize_callback' => 'fanspaysite_sanitize_archive_display',
		'transport'         => 'refresh',
	) );
	$wp_customize->add_control( 'fanspaysite_archive_display', array(
		'label'       => __( 'Archive Display Mode', 'fanspaysite' ),
		'section'     => 'fanspaysite_post_display_section',
		'type'        => 'radio',
		'choices'     => array(
			'classic'  => __( 'List (classic)', 'fanspaysite' ),
			'fullpage' => __( 'Fullpage (snap scroll, one post per screen)', 'fanspaysite' ),
			'boxes'    => __( 'Boxes (grid cards)', 'fanspaysite' ),
		),
		'description' => __( 'Choose how posts appear on archive/blog listing pages.', 'fanspaysite' ),
		'priority'    => 1,
	) );

	$post_details = array(
		'display_title'    => __( 'Display Post Title', 'fanspaysite' ),
		'display_category' => __( 'Display Post Category', 'fanspaysite' ),
		'display_tags'     => __( 'Display Post Tags', 'fanspaysite' ),
		'display_date'     => __( 'Display Post Date', 'fanspaysite' ),
		'display_author'   => __( 'Display Post Author', 'fanspaysite' ),
	);

	foreach ( $post_details as $setting => $label ) {
		$wp_customize->add_setting( $setting, array(
			'default'           => true,
			'sanitize_callback' => 'fanspaysite_sanitize_checkbox',
		) );
		$wp_customize->add_control( $setting, array(
			'label'   => $label,
			'section' => 'fanspaysite_post_display_section',
			'type'    => 'checkbox',
		) );
	}

	// Default thumbnail
	$wp_customize->add_setting( 'default_post_thumbnail', array(
		'default'           => get_template_directory_uri() . '/default.png',
		'sanitize_callback' => 'esc_url_raw',
	) );
	$wp_customize->add_control( new WP_Customize_Image_Control( $wp_customize, 'default_post_thumbnail', array(
		'label'   => __( 'Default Post Thumbnail', 'fanspaysite' ),
		'section' => 'fanspaysite_post_display_section',
	) ) );

	// =====================================================
	// Footer Section
	// =====================================================
	$wp_customize->add_setting( 'fanspaysite_footer_columns', array(
		'default'           => 1,
		'sanitize_callback' => 'absint',
	) );
	$wp_customize->add_control( 'fanspaysite_footer_columns', array(
		'label'   => __( 'Footer Widget Columns', 'fanspaysite' ),
		'section' => 'fanspaysite_footer_section',
		'type'    => 'select',
		'choices' => array(
			0 => __( 'None', 'fanspaysite' ),
			1 => __( '1 Column', 'fanspaysite' ),
			2 => __( '2 Columns', 'fanspaysite' ),
			3 => __( '3 Columns', 'fanspaysite' ),
		),
	) );

	$wp_customize->add_setting( 'fanspaysite_show_copyright', array(
		'default'           => true,
		'sanitize_callback' => 'fanspaysite_sanitize_checkbox',
		'transport'         => 'refresh',
	) );
	$wp_customize->add_control( 'fanspaysite_show_copyright', array(
		'label'   => __( 'Show Copyright Notice', 'fanspaysite' ),
		'section' => 'fanspaysite_footer_section',
		'type'    => 'checkbox',
	) );
}
add_action( 'customize_register', 'fanspaysite_customize_register' );


// =====================================================
// Sanitization Callbacks
// =====================================================

function fanspaysite_sanitize_theme_mode( $input ) {
	$valid = array( 'light', 'dark', 'auto' );
	return in_array( $input, $valid, true ) ? $input : 'light';
}

function fanspaysite_sanitize_show_switcher( $input ) {
	return ( $input === 'yes' || $input === true ) ? 'yes' : 'no';
}

function fanspaysite_sanitize_checkbox( $checked ) {
	return ( isset( $checked ) && true == $checked ) ? true : false;
}

function fanspaysite_sanitize_layout( $input ) {
	$valid = array( 'left-sidebar', 'top-bar' );
	return in_array( $input, $valid, true ) ? $input : 'left-sidebar';
}

function fanspaysite_sanitize_menu_display_mode( $input ) {
	$valid = array( 'big_icon', 'small_icon_text', 'auto', 'auto_hide' );
	return in_array( $input, $valid, true ) ? $input : 'auto';
}

function fanspaysite_sanitize_submenu_hover( $input ) {
	$valid = array( 'yes', 'no', 'auto' );
	return in_array( $input, $valid, true ) ? $input : 'auto';
}

function fanspaysite_sanitize_burger_menu( $input ) {
	$valid = array( 'yes', 'no', 'auto' );
	return in_array( $input, $valid, true ) ? $input : 'auto';
}

function fanspaysite_sanitize_submenu_display_mode( $input ) {
	$valid = array( 'big_icon', 'small_icon_text', 'auto', 'burger' );
	return in_array( $input, $valid, true ) ? $input : 'auto';
}

function fanspaysite_sanitize_content_width( $input ) {
	$valid = array( 'default', 'wide', 'boxed' );
	return in_array( $input, $valid, true ) ? $input : 'default';
}

function fanspaysite_sanitize_right_sidebar( $input ) {
	$valid = array( 'auto', 'yes', 'no' );
	return in_array( $input, $valid, true ) ? $input : 'auto';
}

function fanspaysite_sanitize_font_preset( $input ) {
	$valid = array( 'system', 'serif', 'modern', 'rounded' );
	return in_array( $input, $valid, true ) ? $input : 'system';
}

function fanspaysite_sanitize_archive_display( $input ) {
	$valid = array( 'classic', 'fullpage', 'boxes' );
	return in_array( $input, $valid, true ) ? $input : 'classic';
}
