<?php
/**
 * File Path: templates/partials/section-book-club-resources.php
 *
 * My Book Showroom — Book Club Resources Section (PRO)
 *
 * Renders discussion guide, discussion questions (ordered list), and
 * downloadable files for a single book page.
 *
 * GP-UI-1: returns early with no output when PRO gate fails, when the
 * author has toggled visibility off, or when all content fields are empty.
 *
 * File data: items are read from MBS_Book_Club_DAO::get_files() which always
 * returns {id: int, label: string} objects. Legacy plain-int fallback is
 * retained in the render loop for backward compatibility.
 *
 * Section key: 'book-club-resources'
 *
 * @package MyBookShowroom
 * @since   1.0.0
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}
// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound -- template-scope variables injected by mbs_get_template_part(); not global vars

if ( ! mbs_is_pro() ) {
	MBS_Pro_Gate::comment( 'Book Club Resources' );
	return;
}

if ( ! isset( $post_id ) ) {
	$post_id = get_the_ID();
}

// Phase 9: read from DAO (mbs_book_club / questions / files tables).
$_bc_header = MBS_Book_Club_DAO::get_header( $post_id );

// Visibility gate — hidden if the author has toggled off front-end display.
if ( ! $_bc_header['enabled'] ) {
	return;
}

$guide     = $_bc_header['guide'];
$questions = MBS_Book_Club_DAO::get_questions( $post_id );
$files     = MBS_Book_Club_DAO::get_files( $post_id );

// GP-UI-1: return early if there is nothing to render.
$has_guide     = '' !== trim( (string) $guide );
$has_questions = ! empty( $questions );
$has_files     = ! empty( $files );

if ( ! $has_guide && ! $has_questions && ! $has_files ) {
	return;
}
?>

<div class="mbs-wrapper mbs-section mbs-section-book-club">
	<h2><?php esc_html_e( 'Book Club Resources', 'my-book-showroom' ); ?></h2>

	<?php if ( $has_guide ) : ?>
	<div class="mbs-book-club-guide">
		<h3><?php esc_html_e( 'Discussion Guide', 'my-book-showroom' ); ?></h3>
		<div class="mbs-book-club-guide-content">
			<?php echo wp_kses_post( $guide ); ?>
		</div>
	</div>
	<?php endif; ?>

	<?php if ( $has_questions ) : ?>
	<div class="mbs-book-club-questions">
		<h3><?php esc_html_e( 'Discussion Questions', 'my-book-showroom' ); ?></h3>
		<ol class="mbs-book-club-question-list">
			<?php foreach ( $questions as $question ) : ?>
			<li><?php echo esc_html( $question ); ?></li>
			<?php endforeach; ?>
		</ol>
	</div>
	<?php endif; ?>

	<?php if ( $has_files ) : ?>
	<div class="mbs-book-club-files">
		<h3><?php esc_html_e( 'Downloads', 'my-book-showroom' ); ?></h3>
		<ul class="mbs-book-club-file-list">
			<?php foreach ( $files as $item ) :
				// Backward-compat: old format was plain int, new is {id, label}.
				if ( is_array( $item ) ) {
					$att_id     = absint( $item['id']    ?? 0 );
					$file_label = sanitize_text_field( $item['label'] ?? '' );
				} else {
					$att_id     = absint( $item );
					$file_label = '';
				}
				if ( ! $att_id ) {
					continue;
				}
				$url = wp_get_attachment_url( $att_id );
				if ( ! $url ) {
					continue;
				}
				$filename   = basename( (string) get_attached_file( $att_id ) );
				$link_label = '' !== $file_label ? $file_label : $filename;
			?>
			<li class="mbs-book-club-file-item">
				<a href="<?php echo esc_url( $url ); ?>"
				   download
				   class="mbs-book-club-file-link">
					<span class="dashicons dashicons-download" aria-hidden="true"></span>
					<?php echo esc_html( $link_label ); ?>
				</a>
			</li>
			<?php endforeach; ?>
		</ul>
	</div>
	<?php endif; ?>

</div>
