<?php
/**
 * File Path: templates/partials/section-reader-platforms.php
 *
 * My Book Showroom -- Reader Platforms Links (Sprint 180)
 *
 * Renders shelf / follow buttons for reader-platform links stored as
 * link_type = 'platform' rows in mbs_buy_links (Goodreads, StoryGraph,
 * BookBub, and any custom platforms added by the author).
 *
 * GP-UI-1: section absent when no platform rows carry a URL.
 *          Any URL absent = its button absent.
 *
 * Icon resolution order per button:
 *   1. PRO custom attachment (icon_attachment_id, any image size 24x24).
 *   2. Branded SVG in assets/images/retailers/{key}.svg.
 *   3. No icon (label only).
 *
 * Rendered inside .mbs-external-links-zone in single-mbs-book.php.
 *
 * @package MyBookShowroom
 * @since   4.25.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_registered() ) {
	return;
}

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

/** @var stdClass[] $_platforms */
$_platforms = MBS_Buy_Link_DAO::get_platforms_for_book( (int) $post_id );
$_platforms = array_values(
	array_filter( $_platforms, static function( $_p ) { return ! empty( $_p->url ); } )
);

// GP-UI-1: nothing to render when no platform URLs are set.
if ( empty( $_platforms ) ) {
	return;
}

$_retailers_dir = MBS_DIR . 'assets/images/retailers/';
$_retailers_url = plugins_url( 'assets/images/retailers/', MBS_DIR . 'bookshowroom.php' );

/**
 * Default button labels for well-known reader platforms.
 * Overridden at row level by link_label if set.
 *
 * @var string[]
 */
$_default_labels = array(
	'goodreads'  => __( 'Add to Goodreads',  'my-book-showroom' ),
	'storygraph' => __( 'Add to StoryGraph', 'my-book-showroom' ),
	'bookbub'    => __( 'Follow on BookBub', 'my-book-showroom' ),
);
?>

<div class="mbs-external-links">

	<?php foreach ( $_platforms as $_platform ) : ?>
		<?php
		$_key      = $_platform->retailer_key ?? '';
		$_label    = ( ! empty( $_platform->link_label ) ) ? (string) $_platform->link_label : '';
		if ( '' === $_label ) {
			$_label = $_default_labels[ $_key ] ?? ucwords( str_replace( '-', ' ', $_key ) );
		}

		// Icon resolution.
		$_icon_url = '';
		if ( ! empty( $_platform->icon_attachment_id ) && mbs_is_pro() ) {
			$_icon_url = (string) wp_get_attachment_image_url( // R5-exempt: retailer platform icon (icon_attachment_id), not a book cover
				(int) $_platform->icon_attachment_id,
				array( 24, 24 )
			);
		}
		if ( '' === $_icon_url && '' !== $_key ) {
			if ( file_exists( $_retailers_dir . $_key . '.svg' ) ) {
				$_icon_url = $_retailers_url . rawurlencode( $_key ) . '.svg';
			}
		}
		?>
		<a
			href="<?php echo esc_url( $_platform->url ); ?>"
			class="mbs-external-link-btn mbs-external-link-btn--<?php echo esc_attr( $_key ?: 'custom' ); ?>"
			target="_blank"
			rel="noopener noreferrer"
		>
			<?php if ( $_icon_url ) : ?>
				<img
					src="<?php echo esc_url( $_icon_url ); ?>"
					width="24"
					height="24"
					alt=""
					aria-hidden="true"
					class="mbs-platform-icon"
				/>
			<?php endif; ?>
			<?php echo esc_html( $_label ); ?>
		</a>
	<?php endforeach; ?>

</div>
