<?php
/**
 * File Path: templates/partials/power-badge.php
 *
 * My Book Showroom — Power Badge Partial
 *
 * Renders the "Powered by My Book Showroom" badge on front-end MBS pages.
 *
 * Gate logic (in order):
 *   1. If ! mbs_is_registered() -- return (no MBS context on page).
 *   2. If mbs_is_pro():
 *        Read mbs_setting_appearance_badge_enabled (default: true).
 *        If explicitly disabled -- return (PRO/PUB opt-out, on by default).
 *   3. FREE tier: always render (mandatory, no opt-out).
 *
 * Position: mbs_setting_appearance_badge_position
 *   Values: bottom-right (default), bottom-left, bottom-center
 *
 * Anti-tamper: inline style="display:block !important;" on wrapper div.
 * File is included in the API server file-integrity manifest.
 * Do NOT modify without coordinating a manifest rehash on the API server.
 *
 * @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

// Gate 1: No MBS license registered -- suppress entirely.
if ( ! mbs_is_registered() ) {
	return;
}

// Gate 2: PRO/PUB tier -- opt-out (default ON; FREE cannot opt out).
$mbs_badge_enabled = true;
if ( mbs_is_pro() ) {
	$mbs_badge_enabled = (bool) mbs_get_setting( 'appearance', 'badge_enabled', true );
	if ( ! $mbs_badge_enabled ) {
		return;
	}
}

// Resolve badge position.
$mbs_badge_position  = (string) mbs_get_setting( 'appearance', 'badge_position', 'bottom-right' );
$mbs_allowed_positions = array( 'bottom-right', 'bottom-left', 'bottom-center' );
if ( ! in_array( $mbs_badge_position, $mbs_allowed_positions, true ) ) {
	$mbs_badge_position = 'bottom-right';
}

// Determine tier.
$mbs_badge_tier = 'free';
if ( mbs_is_pub() ) {
	$mbs_badge_tier = 'pub';
} elseif ( mbs_is_pro() ) {
	$mbs_badge_tier = 'pro';
}

// Badge text by tier.
$mbs_badge_suffix = '';
if ( 'pro' === $mbs_badge_tier ) {
	$mbs_badge_suffix = ' <em>' . esc_html__( 'Professional', 'my-book-showroom' ) . '</em>';
} elseif ( 'pub' === $mbs_badge_tier ) {
	$mbs_badge_suffix = ' <em>' . esc_html__( 'Publisher Edition', 'my-book-showroom' ) . '</em>';
}

$mbs_badge_position_class = 'mbs-power-badge--' . esc_attr( $mbs_badge_position );
$mbs_badge_tier_class     = 'mbs-power-badge--' . esc_attr( $mbs_badge_tier );
$mbs_badge_url            = defined( 'MBS_WEBSITE_URL' ) ? MBS_WEBSITE_URL : 'https://www.dmrstech.com';
?>
<div class="mbs-power-badge <?php echo esc_attr( $mbs_badge_tier_class ); ?> <?php echo esc_attr( $mbs_badge_position_class ); ?>" style="display:block !important;">
	<a href="<?php echo esc_url( $mbs_badge_url ); ?>" target="_blank" rel="noopener noreferrer" class="mbs-power-badge__link">
		<svg class="mbs-power-badge__icon" viewBox="0 0 16 12" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false">
			<path fill="currentColor" d="M8 2.5C6 1.3 2.5.8 0 1.2L0 10.5C2.5 10.1 6 10.6 8 11.5 9.4 10.6 13 10.1 16 10.5L16 1.2C13.5.8 10 1.3 8 2.5Z"/>
		</svg>
		<span class="mbs-power-badge__text"><?php esc_html_e( 'Powered by My Book Showroom', 'my-book-showroom' ); ?><?php
		if ( '' !== $mbs_badge_suffix ) {
			echo wp_kses( $mbs_badge_suffix, array( 'em' => array() ) ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- kses-sanitized
		}
		?></span>
	</a>
</div>
