<?php
/**
 * File Path: includes/admin/tabs/appearance/class-mbs-appearance-analytics-render.php
 *
 * Analytics Settings — Render Class
 *
 * Called statically from MBS_Tab_Advanced_Render.
 * Originally on Appearance tab; moved to Advanced tab (R-ADV-02, Session 183).
 *
 * @package MyBookShowroom
 * @since   1.0.0
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

class MBS_Appearance_Analytics_Render { // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedClassFound -- mbs_/MBS_ prefix is correct; scanner not configured with plugin prefix

	/**
	 * Render the Analytics settings collapsible section (Spec 2 / Cookie Analytics).
	 *
	 * All tiers. Supports GA4 (gtag.js) and Plausible.io.
	 * Tracking ID is stored AES-256-CBC encrypted; decrypted here for display.
	 * Saved via MBS_Tab_Advanced::save_settings() (R-ADV-02).
	 *
	 * @return void
	 */
	public static function render(): void {
		$enabled  = (bool) get_option( 'mbs_setting_analytics_enabled', 0 );
		$provider = get_option( 'mbs_setting_analytics_provider', 'ga4' );
		$id_enc   = get_option( 'mbs_setting_analytics_id', '' );
		$id_plain = ( '' !== $id_enc ) ? MBS_Encryption::decrypt( $id_enc ) : '';
		if ( false === $id_plain ) {
			$id_plain = ''; // Decryption failure -- treat as empty.
		}
		?>
		<div class="mbs-appearance-section mbs-settings-card mbs-layout-customization-wrap">
			<button type="button"
			        class="mbs-accordion-toggle"
			        aria-expanded="false"
			        aria-controls="mbs-analytics-body">
				<span class="mbs-accordion-icon" aria-hidden="true">&#9658;</span>
				<?php esc_html_e( 'Analytics', 'my-book-showroom' ); ?>
			</button>

			<div id="mbs-analytics-body" class="mbs-accordion-body" hidden>
				<table class="form-table">

					<!-- Enable toggle -->
					<tr>
						<th scope="row">
							<label for="mbs_analytics_enabled">
								<?php esc_html_e( 'Enable Analytics', 'my-book-showroom' ); ?>
							</label>
						</th>
						<td>
							<input type="checkbox"
							       id="mbs_analytics_enabled"
							       name="mbs_setting_analytics_enabled"
							       value="1"
							       <?php checked( $enabled ); ?>>
							<p class="description">
								<?php esc_html_e( 'Inject an analytics script into the &lt;head&gt; of your book pages.', 'my-book-showroom' ); ?>
							</p>
						</td>
					</tr>

					<!-- Provider selector -->
					<tr>
						<th scope="row">
							<label for="mbs_analytics_provider">
								<?php esc_html_e( 'Analytics Provider', 'my-book-showroom' ); ?>
							</label>
						</th>
						<td>
							<select id="mbs_analytics_provider" name="mbs_setting_analytics_provider">
								<option value="ga4"<?php selected( $provider, 'ga4' ); ?>><?php esc_html_e( 'Google Analytics 4 (GA4)', 'my-book-showroom' ); ?></option>
								<option value="plausible"<?php selected( $provider, 'plausible' ); ?>><?php esc_html_e( 'Plausible.io', 'my-book-showroom' ); ?></option>
							</select>
						</td>
					</tr>

					<!-- Tracking ID / domain -->
					<tr>
						<th scope="row">
							<span class="mbs-label-tip-pair"><label for="mbs_analytics_id">
								<span class="mbs-analytics-id-label-ga4"><?php esc_html_e( 'Measurement ID (G-XXXXXXX)', 'my-book-showroom' ); ?></span>
								<span class="mbs-analytics-id-label-plausible" style="display:none;"><?php esc_html_e( 'Site Domain (yourdomain.com)', 'my-book-showroom' ); ?></span>
							</label><?php mbs_help_tip(
								__( 'MBS injects the gtag.js or Plausible script tag into your book pages and nothing else. GDPR and CCPA compliance — including the GA4 Consent Mode v2 signals (ad_storage, analytics_storage) and the cookie consent banner that sets those signals — is entirely your responsibility and requires a separate Consent Management Platform (CMP) plugin. MBS does not include a CMP and does not manage consent banners.', 'my-book-showroom' ),
								__( 'Analytics compliance note', 'my-book-showroom' ),
								'mbs-help-tip--wide'
							); ?></span>
						</th>
						<td>
							<input type="text"
							       id="mbs_analytics_id"
							       name="mbs_setting_analytics_id"
							       value="<?php echo esc_attr( $id_plain ); ?>"
							       class="regular-text"
							       autocomplete="off"
							       autocomplete="off">
							<p class="description">
								<?php esc_html_e( 'Stored encrypted. GA4 Consent Mode v2 is your responsibility — MBS injects the script only.', 'my-book-showroom' ); ?>
							</p>
						</td>
					</tr>

				</table>
			</div><!-- .mbs-accordion-body -->
		</div><!-- .mbs-analytics-wrap -->
		<?php
	}

}
