<?php
/**
 * FansPaysite Customizer settings.
 *
 * Organized into a panel with sections: Layout, Header, Colors,
 * Typography, Posts, Pages, 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'    => __( 'Posts', 'fanspaysite' ),
		'panel'    => 'fanspaysite_panel',
		'priority' => 50,
	) );

	$wp_customize->add_section( 'fanspaysite_pages_section', array(
		'title'    => __( 'Pages', 'fanspaysite' ),
		'panel'    => 'fanspaysite_panel',
		'priority' => 55,
	) );

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

	// =====================================================
	// Title & Tagline (WP default section)
	// =====================================================
	// Dark Mode Logo
	$wp_customize->add_setting( 'fanspaysite_dark_logo', array(
		'default'           => '',
		'transport'         => 'refresh',
		'sanitize_callback' => 'esc_url_raw',
	) );
	$wp_customize->add_control( new WP_Customize_Image_Control( $wp_customize, 'fanspaysite_dark_logo', array(
		'label'       => __( 'Dark Mode Logo', 'fanspaysite' ),
		'description' => __( 'Shown instead of the main logo when dark mode is active. Leave empty to use the same logo in both modes.', 'fanspaysite' ),
		'section'     => 'title_tagline',
	) ) );

	// Contrast Logo
	$wp_customize->add_setting( 'fanspaysite_contrast_logo', array(
		'default'           => false,
		'transport'         => 'refresh',
		'sanitize_callback' => 'fanspaysite_sanitize_checkbox',
	) );
	$wp_customize->add_control( 'fanspaysite_contrast_logo', array(
		'label'       => __( 'Contrast Logo', 'fanspaysite' ),
		'description' => __( 'Applies a subtle outline to the logo to improve visibility against any background color.', 'fanspaysite' ),
		'section'     => 'title_tagline',
		'type'        => 'checkbox',
	) );

	$wp_customize->add_setting( 'display_site_title', array(
		'default'           => 'auto',
		'transport'         => 'refresh',
		'sanitize_callback' => 'fanspaysite_sanitize_title_display',
	) );
	$wp_customize->add_control( 'display_site_title', array(
		'label'       => __( 'Display Site Title', 'fanspaysite' ),
		'description' => __( 'Auto: shown only when no logo is set.', 'fanspaysite' ),
		'section'     => 'title_tagline',
		'type'        => 'select',
		'choices'     => array(
			'auto' => __( 'Auto (show when no logo)', 'fanspaysite' ),
			'yes'  => __( 'Yes (always show)', 'fanspaysite' ),
			'no'   => __( 'No (always hide)', 'fanspaysite' ),
		),
	) );

	$wp_customize->add_setting( 'display_site_tagline', array(
		'default'           => 'auto',
		'transport'         => 'refresh',
		'sanitize_callback' => 'fanspaysite_sanitize_title_display',
	) );
	$wp_customize->add_control( 'display_site_tagline', array(
		'label'       => __( 'Display Site Tagline', 'fanspaysite' ),
		'description' => __( 'Auto: shown only when no logo is set.', 'fanspaysite' ),
		'section'     => 'title_tagline',
		'type'        => 'select',
		'choices'     => array(
			'auto' => __( 'Auto (show when no logo)', 'fanspaysite' ),
			'yes'  => __( 'Yes (always show)', 'fanspaysite' ),
			'no'   => __( 'No (always hide)', 'fanspaysite' ),
		),
	) );

	// =====================================================
	// 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'           => 'no',
		'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,
	) );

	$wp_customize->add_setting( 'fanspaysite_show_back_to_top', array(
		'default'           => true,
		'sanitize_callback' => 'fanspaysite_sanitize_checkbox',
	) );
	$wp_customize->add_control( 'fanspaysite_show_back_to_top', array(
		'label'       => __( 'Back to Top Button', 'fanspaysite' ),
		'section'     => 'fanspaysite_layout_section',
		'type'        => 'checkbox',
		'description' => __( 'Show a floating arrow button in the bottom-right corner when the page can scroll and you are away from the top.', 'fanspaysite' ),
		'priority'    => 5,
	) );

	// =====================================================
	// 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' => '#18122B',
			'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' ),
			'modern'       => __( 'Modern (Inter)', 'fanspaysite' ),
			'rounded'      => __( 'Rounded (Nunito)', 'fanspaysite' ),
			'lato'         => __( 'Clean (Lato)', 'fanspaysite' ),
			'roboto'       => __( 'Neutral (Roboto)', 'fanspaysite' ),
			'open_sans'    => __( 'Friendly (Open Sans)', 'fanspaysite' ),
			'poppins'      => __( 'Geometric (Poppins)', 'fanspaysite' ),
			'serif'        => __( 'Serif (Georgia)', 'fanspaysite' ),
			'merriweather' => __( 'Editorial Serif (Merriweather)', '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',
	) ) );

	// =====================================================
	// Pages Section
	// =====================================================
	$wp_customize->add_setting( 'fanspaysite_page_show_title', array(
		'default'           => true,
		'sanitize_callback' => 'fanspaysite_sanitize_checkbox',
	) );
	$wp_customize->add_control( 'fanspaysite_page_show_title', array(
		'label'   => __( 'Show Title', 'fanspaysite' ),
		'section' => 'fanspaysite_pages_section',
		'type'    => 'checkbox',
	) );

	$wp_customize->add_setting( 'fanspaysite_page_title_alignment', array(
		'default'           => 'left',
		'sanitize_callback' => 'fanspaysite_sanitize_page_title_alignment',
	) );
	$wp_customize->add_control( 'fanspaysite_page_title_alignment', array(
		'label'   => __( 'Title Alignment', 'fanspaysite' ),
		'section' => 'fanspaysite_pages_section',
		'type'    => 'select',
		'choices' => array(
			'left'   => __( 'Left', 'fanspaysite' ),
			'center' => __( 'Center', 'fanspaysite' ),
			'right'  => __( 'Right', 'fanspaysite' ),
		),
	) );

	$wp_customize->add_setting( 'fanspaysite_page_title_size', array(
		'default'           => 'normal',
		'sanitize_callback' => 'fanspaysite_sanitize_page_title_size',
	) );
	$wp_customize->add_control( 'fanspaysite_page_title_size', array(
		'label'   => __( 'Title Size', 'fanspaysite' ),
		'section' => 'fanspaysite_pages_section',
		'type'    => 'select',
		'choices' => array(
			'big'    => __( 'Big', 'fanspaysite' ),
			'normal' => __( 'Normal', 'fanspaysite' ),
			'small'  => __( 'Small', 'fanspaysite' ),
			'tiny'   => __( 'Tiny', 'fanspaysite' ),
		),
	) );

	// =====================================================
	// 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',
	) );

	$wp_customize->add_setting( 'fanspaysite_show_footer_menu', array(
		'default'           => true,
		'sanitize_callback' => 'fanspaysite_sanitize_checkbox',
		'transport'         => 'refresh',
	) );
	$wp_customize->add_control( 'fanspaysite_show_footer_menu', array(
		'label'       => __( 'Show Footer Menu', 'fanspaysite' ),
		'description' => __( 'Displays a small footer menu for links such as Privacy Policy and Terms of Use.', '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_title_display( $input ) {
	$valid = array( 'auto', 'yes', 'no' );
	return in_array( $input, $valid, true ) ? $input : 'auto';
}

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 : 'no';
}

function fanspaysite_sanitize_font_preset( $input ) {
	$valid = array( 'system', 'modern', 'rounded', 'lato', 'roboto', 'open_sans', 'poppins', 'serif', 'merriweather' );
	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';
}

function fanspaysite_sanitize_page_title_alignment( $input ) {
	$valid = array( 'left', 'center', 'right' );
	return in_array( $input, $valid, true ) ? $input : 'left';
}

function fanspaysite_sanitize_page_title_size( $input ) {
	$valid = array( 'big', 'normal', 'small', 'tiny' );
	return in_array( $input, $valid, true ) ? $input : 'normal';
}
