<?php
/**
 * @file    templates/partials/section-wc-product.php
 * WooCommerce add-to-cart section for single book pages (PRO only).
 * @package MyBookShowroom
 * @since   1.1.0
 */

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

// PRO gate — FREE tier: HTML comment only, no visible element.
if ( ! mbs_is_pro() ) {
	MBS_Pro_Gate::comment( 'WooCommerce integration' );
	return;
}

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

// WC integration must be enabled for this book.
$wc_enabled = (bool) get_post_meta( $post_id, 'mbs_wc_enabled', true );
if ( ! $wc_enabled ) {
	return;
}

// WooCommerce must be active.
if ( ! class_exists( 'WooCommerce' ) ) {
	return;
}

$product = MBS_WooCommerce_Bridge::get_product_for_book( (int) $post_id );
if ( null === $product ) {
	return;
}

$in_stock    = $product->is_in_stock();
$stock_label = $in_stock
	? __( 'In Stock', 'my-book-showroom' )
	: __( 'Out of Stock', 'my-book-showroom' );
$stock_class = $in_stock ? 'mbs-wc-in-stock' : 'mbs-wc-out-of-stock';
?>
<div class="mbs-wc-add-to-cart" data-wc-product-id="<?php echo esc_attr( $product->get_id() ); ?>">
	<span class="mbs-wc-stock-status <?php echo esc_attr( $stock_class ); ?>">
		<?php echo esc_html( $stock_label ); ?>
	</span>
	<span class="mbs-wc-product-price" aria-live="polite">
		<?php echo wp_kses_post( $product->get_price_html() ); ?>
	</span>
	<?php if ( $in_stock ) : ?>
		<?php wc_get_template_part( 'single-product/add-to-cart/simple' ); ?>
	<?php endif; ?>
</div>
