<?php

// Add theme support for essential features
function fanspaysite_setup() {
	add_theme_support( 'post-thumbnails' );
	add_theme_support( 'automatic-feed-links' );
	add_theme_support( 'title-tag' );
	add_theme_support( 'html5', array( 'search-form', 'comment-form', 'comment-list', 'gallery', 'caption', ) );
	add_theme_support( 'customize-selective-refresh-widgets' );


	add_theme_support('custom-logo', array(
		'height'      => 128, // Set the desired height for the logo
		'width'       => 128, // Set the desired width for the logo
		'flex-height' => true, // Allow flexible logo height
		'flex-width'  => true, // Allow flexible logo width
	//	'header-text' => array('site-title', 'site-description'), // Select which elements the logo can replace
	));

	add_theme_support( 'wp-block-styles' );
	add_theme_support( 'responsive-embeds');
	add_theme_support( 'align-wide' );

	add_theme_support( 'custom-header', array(
		'width'         => 1920,
		'height'        => 128,
		'flex-width'    => true,
		'flex-height'   => true,
		'default-image' => '', // Path to default header image
		'header-text'   => false, // Set to true if you want text over the header image
	));

	add_theme_support( 'custom-background', array(
		'default-color' => 'fff', // Default background color
		'default-image' => '', // Path to default background image, if any
		'wp-head-callback' => '_custom_background_cb',
	));

	add_theme_support( 'post-formats', array( 'aside', 'gallery', 'image', 'video', 'quote', 'link', 'audio' ) );

	load_theme_textdomain('fanspaysite', get_template_directory() . '/languages');
}
add_action( 'after_setup_theme', 'fanspaysite_setup' );

function fanspaysite_register_sidebars() {
	register_sidebar(array(
		'id'            => 'fanspaysite_left_sidebar',
		'name'          => __('FansPaysite Left Sidebar', 'fanspaysite'),
		'description'   => __('Widgets added here will appear in the left column of the site, under menu.', 'fanspaysite'),
		'before_widget' => '<section id="%1$s" class="widget %2$s">',
		'after_widget'  => '</section>',
		'before_title'  => '<h2 class="widget-title">',
		'after_title'   => '</h2>',
	));
	register_sidebar(array(
		'id'            => 'fanspaysite_right_sidebar',
		'name'          => __('FansPaysite Right Sidebar', 'fanspaysite'),
		'description'   => __('Widgets added here will appear in the right column of the site, when visible.', 'fanspaysite'),
		'before_widget' => '<section id="%1$s" class="widget %2$s">',
		'after_widget'  => '</section>',
		'before_title'  => '<h2 class="widget-title">',
		'after_title'   => '</h2>',
	));
	register_sidebar(array(
		'id'            => 'fanspaysite_menu_widgets',
		'name'          => __('FansPaysite Menu Widgets', 'fanspaysite'),
		'description'   => __('Optional compact widgets shown with the menu: at the right of the top bar or below the left sidebar menu.', 'fanspaysite'),
		'before_widget' => '<section id="%1$s" class="widget fps-menu-widget %2$s">',
		'after_widget'  => '</section>',
		'before_title'  => '<h2 class="widget-title">',
		'after_title'   => '</h2>',
	));

	// Footer widget areas (up to 3 columns)
	for ( $i = 1; $i <= 3; $i++ ) {
		register_sidebar( array(
			'id'            => 'fanspaysite_footer_' . $i,
			'name'          => sprintf( __( 'Footer Column %d', 'fanspaysite' ), $i ),
			'description'   => sprintf( __( 'Widgets for footer column %d.', 'fanspaysite' ), $i ),
			'before_widget' => '<section id="%1$s" class="widget %2$s">',
			'after_widget'  => '</section>',
			'before_title'  => '<h2 class="widget-title">',
			'after_title'   => '</h2>',
		) );
	}
}
add_action('widgets_init', 'fanspaysite_register_sidebars');

// Add body classes for menu display mode, submenu display, hover mode, and layout
function fanspaysite_body_classes($classes) {
	$menu_mode = get_theme_mod('fanspaysite_menu_display_mode', 'auto');
	$classes[] = 'menu-display-' . sanitize_html_class($menu_mode);

	$submenu_mode = get_theme_mod('fanspaysite_submenu_display_mode', 'auto');
	$classes[] = 'submenu-display-' . sanitize_html_class($submenu_mode);

	$hover_mode = get_theme_mod('fanspaysite_submenu_hover', 'auto');
	$classes[] = 'submenu-hover-' . sanitize_html_class($hover_mode);

	$layout = get_theme_mod('fanspaysite_layout', 'left-sidebar');
	$classes[] = 'layout-' . sanitize_html_class($layout);

	$burger_menu = get_theme_mod('fanspaysite_burger_menu', 'auto');
	$classes[] = 'burger-menu-' . sanitize_html_class($burger_menu);

	$active_skin = get_option( 'fanspaysite_active_skin', 'default' );
	$classes[]   = 'fanspaysite-skin-' . sanitize_html_class( $active_skin );

	$theme_mode = fanspaysite_get_current_theme_mode();
	$classes[]  = 'fanspaysite-theme-mode-' . sanitize_html_class( $theme_mode );

	// Archive display mode (only on archive-like pages)
	if ( is_home() || is_archive() || is_search() ) {
		$archive_display = get_theme_mod( 'fanspaysite_archive_display', 'classic' );
		$classes[] = 'archive-display-' . sanitize_html_class( $archive_display );
	}

	// Contrast logo
	if ( get_theme_mod( 'fanspaysite_contrast_logo', false ) ) {
		$classes[] = 'fps-contrast-logo';
	}

	return $classes;
}
add_filter('body_class', 'fanspaysite_body_classes');

/**
 * Get theme integration context for plugins and custom code.
 *
 * @since 2.3
 * @return array<string, mixed>
 */
function fanspaysite_get_theme_integration_context() {
	$context = array(
		'active_skin'           => get_option( 'fanspaysite_active_skin', 'default' ),
		'layout'                => get_theme_mod( 'fanspaysite_layout', 'left-sidebar' ),
		'burger_menu'           => get_theme_mod( 'fanspaysite_burger_menu', 'auto' ),
		'menu_display_mode'     => get_theme_mod( 'fanspaysite_menu_display_mode', 'auto' ),
		'submenu_display_mode'  => get_theme_mod( 'fanspaysite_submenu_display_mode', 'auto' ),
		'submenu_hover'         => get_theme_mod( 'fanspaysite_submenu_hover', 'auto' ),
		'theme_mode_preference' => fanspaysite_get_current_theme_mode(),
		'default_theme_mode'    => get_theme_mod( 'fanspaysite_default_theme_mode', 'light' ),
		'color_tokens'          => function_exists( 'fanspaysite_get_theme_color_tokens' ) ? fanspaysite_get_theme_color_tokens() : array(),
		'fomantic_styles'       => function_exists( 'fanspaysite_get_fomantic_style_handles' ) ? fanspaysite_get_fomantic_style_handles() : array(),
		'fomantic_scripts'      => function_exists( 'fanspaysite_get_fomantic_script_handles' ) ? fanspaysite_get_fomantic_script_handles() : array(),
		'font_awesome_style'    => 'font-awesome',
	);

	return apply_filters( 'fanspaysite_theme_integration_context', $context );
}

/**
 * Render light + dark logos.
 *
 * Outputs the logo markup inside a .fps-logo-wrap container. When a dark mode
 * logo URL is provided, both variants are emitted: CSS/data-theme toggles
 * visibility. Returns true if at least one logo was rendered, false otherwise
 * (callers use this value for the Auto title/tagline logic).
 *
 * @since 2.5
 * @return bool
 */
function fanspaysite_render_logos() {
	$has_light = has_custom_logo();
	$dark_url  = get_theme_mod( 'fanspaysite_dark_logo', '' );
	$has_dark  = ! empty( $dark_url );

	if ( ! $has_light && ! $has_dark ) {
		return false;
	}

	$wrap_class = 'fps-logo-wrap';
	if ( $has_dark ) {
		$wrap_class .= ' fps-has-dark-logo';
	}

	echo '<div class="' . esc_attr( $wrap_class ) . '">';

	if ( $has_light ) {
		$logo_id    = (int) get_theme_mod( 'custom_logo' );
		$logo_image = wp_get_attachment_image( $logo_id, 'full', false, array( 'class' => 'custom-logo' ) );
		echo '<a href="' . esc_url( home_url( '/' ) ) . '" class="custom-logo-link fps-logo-light" rel="home" aria-label="' . esc_attr( get_bloginfo( 'name' ) ) . '">' . $logo_image . '</a>';
	}

	if ( $has_dark ) {
		echo '<a href="' . esc_url( home_url( '/' ) ) . '" class="custom-logo-link fps-logo-dark" rel="home" aria-label="' . esc_attr( get_bloginfo( 'name' ) ) . '">'
			. '<img src="' . esc_url( $dark_url ) . '" alt="' . esc_attr( get_bloginfo( 'name' ) ) . '" class="custom-logo">'
			. '</a>';
	}

	echo '</div>';

	return true;
}

/**
 * Get the current archive display mode.
 *
 * @since 2.3
 * @return string 'classic', 'fullpage', or 'boxes'
 */
function fanspaysite_get_archive_display() {
	return get_theme_mod( 'fanspaysite_archive_display', 'classic' );
}

require_once get_template_directory() . '/inc/enqueue.php';
require_once get_template_directory() . '/inc/menu.php';
require_once get_template_directory() . '/inc/mode.php';
require_once get_template_directory() . '/inc/customizer.php';
require_once get_template_directory() . '/inc/touch-icons.php';
require_once get_template_directory() . '/inc/comments.php';
require_once get_template_directory() . '/inc/blocks.php';
require_once get_template_directory() . '/inc/admin.php';
require_once get_template_directory() . '/inc/onboarding.php';

// WooCommerce support — loaded only when WooCommerce is active.
if ( class_exists( 'WooCommerce' ) ) {
	require_once get_template_directory() . '/inc/woocommerce.php';
}

// BuddyPress support — loaded only when BuddyPress is active.
if ( function_exists( 'buddypress' ) ) {
	require_once get_template_directory() . '/inc/buddypress.php';
}
