<?php
/**
 * Forumax – bbPress Wrapper Template for Block / FSE Themes
 *
 * Provides a complete page scaffold that works with block (FSE) themes which do
 * not ship classic PHP template files (header.php, footer.php, etc.).
 *
 * NOTE: We do NOT manually render the sidebar here. Forumax's own templates
 * (content-archive-forum.php, content-single-topic.php, etc.) already render
 * the sidebar via their own layout containers. Rendering it again here would
 * produce duplicate sidebars. We only provide the outer page chrome.
 *
 * @package Forumax
 * @since   2.3.1
 */

defined( 'ABSPATH' ) || exit;

/**
 * Safely render a block template part with fallback.
 *
 * @param string $part The template part name (e.g., 'header', 'footer').
 */
function forumax_render_block_template_part( $part ) {
	// Check if block_template_part function exists (WP 5.9+)
	if ( ! function_exists( 'block_template_part' ) ) {
		return;
	}

	// Check if the template part exists in the theme
	$template_part = get_block_template( get_stylesheet() . '//' . $part, 'wp_template_part' );

	if ( $template_part ) {
		block_template_part( $part );
	} elseif ( 'header' === $part ) {
		// Minimal fallback header
		forumax_minimal_header();
	} elseif ( 'footer' === $part ) {
		// Minimal fallback footer
		forumax_minimal_footer();
	}
}

/**
 * Minimal header fallback when theme has no header template part.
 */
function forumax_minimal_header() {
	?>
	<header class="forumax-fallback-header" style="padding: 20px 40px; border-bottom: 1px solid #eee;">
		<div style="max-width: 1200px; margin: 0 auto;">
			<a href="<?php echo esc_url( home_url( '/' ) ); ?>" style="text-decoration: none; color: inherit;">
				<strong><?php bloginfo( 'name' ); ?></strong>
			</a>
		</div>
	</header>
	<?php
}

/**
 * Minimal footer fallback when theme has no footer template part.
 */
function forumax_minimal_footer() {
	?>
	<footer class="forumax-fallback-footer" style="padding: 20px 40px; border-top: 1px solid #eee; margin-top: 40px;">
		<div style="max-width: 1200px; margin: 0 auto; text-align: center; color: #666;">
			&copy; <?php echo esc_html( gmdate( 'Y' ) ); ?> <?php bloginfo( 'name' ); ?>
		</div>
	</footer>
	<?php
}
?><!DOCTYPE html>
<html <?php language_attributes(); ?>>
<head>
<meta charset="<?php bloginfo( 'charset' ); ?>">
<meta name="viewport" content="width=device-width, initial-scale=1">
<?php wp_head(); ?>
</head>
<body <?php body_class(); ?>>
<?php wp_body_open(); ?>

<?php forumax_render_block_template_part( 'header' ); ?>

<main id="frmx-bbpress-main" class="frmx-bbpress-page">
	<?php
	// Check if bbPress has content to display
	if ( function_exists( 'bbp_is_single_forum' ) && ( bbp_is_single_forum() || bbp_is_single_topic() || bbp_is_single_reply() || bbp_is_topic_archive() || bbp_is_forum_archive() || bbp_is_single_user() || bbp_is_single_view() || bbp_is_search() || bbp_is_topic_tag() ) ) {
		// Use bbPress content
		while ( have_posts() ) :
			the_post();
			the_content();
		endwhile;
	} else {
		// Fallback - just show the content
		while ( have_posts() ) :
			the_post();
			the_content();
		endwhile;
	}
	?>
</main>

<?php forumax_render_block_template_part( 'footer' ); ?>

<?php wp_footer(); ?>
</body>
</html>
