<?php
/**
 * Reading UI: single guide view. Expects $post (bw_guide, publish) from
 * BW_Guides_Admin::render_browse_page().
 */

defined( 'ABSPATH' ) || exit;

$bw_is_hub = 'hub' === get_post_meta( $post->ID, BW_Guides_CPT::SOURCE_META, true );
$bw_tags   = wp_get_object_terms( $post->ID, BW_Guides_CPT::TAXONOMY, array( 'fields' => 'names' ) );
$bw_tags   = is_wp_error( $bw_tags ) ? array() : $bw_tags;
$bw_note   = (string) get_post_meta( $post->ID, '_bw_guides_notes', true );
?>
<div class="wrap bw-guides-wrap bw-guides-single" data-bw-guide-id="<?php echo (int) $post->ID; ?>" data-bw-guide-modified="<?php echo esc_attr( $post->post_modified_gmt ); ?>">
	<p><a href="<?php echo esc_url( BW_Guides_Admin::list_url() ); ?>">&larr; <?php esc_html_e( 'All guides', 'bw-guides' ); ?></a></p>

	<div class="bw-guides-single-head">
		<?php if ( $bw_is_hub ) : ?>
			<span class="bw-guides-badge bw-guides-badge-hub"><?php esc_html_e( 'Bowden Works', 'bw-guides' ); ?></span>
		<?php else : ?>
			<span class="bw-guides-badge bw-guides-badge-local"><?php esc_html_e( 'Created here', 'bw-guides' ); ?></span>
		<?php endif; ?>
		<h1><?php echo esc_html( $post->post_title ); ?></h1>
		<p class="bw-guides-meta">
			<?php
			/* translators: %s: human-readable time difference */
			echo esc_html( sprintf( __( 'Updated %s ago', 'bw-guides' ), human_time_diff( get_post_modified_time( 'U', true, $post ) ) ) );
			if ( $bw_is_hub ) {
				echo ' &middot; ' . esc_html__( 'Delivered and kept up to date by Bowden Works', 'bw-guides' );
			} elseif ( current_user_can( 'edit_post', $post->ID ) ) {
				echo ' &middot; <a href="' . esc_url( (string) get_edit_post_link( $post->ID ) ) . '">' . esc_html__( 'Edit this guide', 'bw-guides' ) . '</a>';
			}
			?>
		</p>
	</div>

	<?php
	// Renders with do_blocks (not the full the_content chain) deliberately —
	// hub content must never trigger this site's shortcodes or third-party
	// filters — and anchors every heading so sections can be linked directly.
	$bw_prepared = BW_Guides_Content::prepare( $post->post_content );
	$bw_toc      = BW_Guides_Content::contents_list( $bw_prepared['headings'] );
	?>

	<?php if ( '' !== $bw_toc ) : ?>
		<?php echo wp_kses_post( $bw_toc ); ?>
	<?php endif; ?>

	<div class="bw-guides-content">
		<?php
		// Already passed through wp_kses_post inside prepare(); the only
		// addition since is our own generated heading ids.
		echo wp_kses_post( $bw_prepared['html'] );
		?>
	</div>

	<?php
	// Tags and notes WRITE (shared taxonomy terms; a note on the guide itself),
	// so they're gated by the manage capability. A read-only viewer sees the
	// guide without controls that would only fail on save — which reads as a
	// bug rather than as a boundary.
	if ( current_user_can( BW_Guides_Admin::manage_cap() ) ) :
	?>

	<div class="bw-guides-panel">
		<h2><?php esc_html_e( 'Tags', 'bw-guides' ); ?></h2>
		<p class="description"><?php esc_html_e( 'Tag this guide to make it easier to find. Separate tags with commas.', 'bw-guides' ); ?></p>
		<p>
			<input type="text" id="bw-guides-tags-input" class="regular-text" data-post-id="<?php echo (int) $post->ID; ?>" value="<?php echo esc_attr( implode( ', ', $bw_tags ) ); ?>">
			<button type="button" class="button" id="bw-guides-save-tags"><?php esc_html_e( 'Save Tags', 'bw-guides' ); ?></button>
			<span class="bw-guides-feedback" id="bw-guides-tags-feedback" role="status"></span>
		</p>
	</div>

	<div class="bw-guides-panel">
		<h2><?php esc_html_e( 'My Notes', 'bw-guides' ); ?></h2>
		<p class="description"><?php esc_html_e( 'Private notes for your team — these stay on this site and survive guide updates.', 'bw-guides' ); ?></p>
		<textarea id="bw-guides-note-input" class="large-text" rows="5" data-post-id="<?php echo (int) $post->ID; ?>"><?php echo esc_textarea( $bw_note ); ?></textarea>
		<p>
			<button type="button" class="button" id="bw-guides-save-note"><?php esc_html_e( 'Save Notes', 'bw-guides' ); ?></button>
			<span class="bw-guides-feedback" id="bw-guides-note-feedback" role="status"></span>
		</p>
	</div>

	<?php endif; ?>
</div>
