<?php
/**
 * File Path: includes/admin/tabs/class-mbs-tab-affiliate-render.php
 *
 * Earn More Tab — Render Class
 *
 * @package    My_Book_Showroom
 * @subpackage Includes/Admin/Tabs
 * @since      1.0.0
 */

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

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

	/**
	 * Render the Earn More (Affiliate) tab.
	 *
	 * Outputs the tab wrapper, optional PRO upgrade notice, the retailer
	 * affiliate code form, and the custom stores section.
	 *
	 * @return void
	 */
	public static function render(): void {
		$is_pro = mbs_is_pro();

		$amazon      = mbs_get_setting( 'affiliate', 'amazon',      '' );
		$bn          = mbs_get_setting( 'affiliate', 'bn',          '' );
		$kobo        = mbs_get_setting( 'affiliate', 'kobo',        '' );
		$audible     = mbs_get_setting( 'affiliate', 'audible',     '' );
		$bam         = mbs_get_setting( 'affiliate', 'bam',         '' );
		$bookshop    = mbs_get_setting( 'affiliate', 'bookshop',    '' );
		$waterstones = mbs_get_setting( 'affiliate', 'waterstones', '' );
		$apple       = mbs_get_setting( 'affiliate', 'apple',       '' );
		$google      = mbs_get_setting( 'affiliate', 'google',      '' );
		$powells     = mbs_get_setting( 'affiliate', 'powells',     '' );

		$custom_stores = MBS_Affiliate_Store_DAO::get_all(); // RF-012.
		?>
		<div class="mbs-tab-affiliate">

			<h2><?php esc_html_e( 'Earn More', 'my-book-showroom' ); ?></h2>
			<p class="mbs-settings-subtitle">
				<?php esc_html_e( 'Earn commissions when readers click your buy links. Enter your affiliate codes below and MBS will append them to retailer URLs automatically (PRO).', 'my-book-showroom' ); ?>
			</p>

			<?php if ( ! $is_pro ) : ?>
				<div class="mbs-pro-notice notice notice-info inline">
					<p>
						<strong><?php esc_html_e( 'PRO feature', 'my-book-showroom' ); ?></strong>
						&mdash;
						<?php esc_html_e( 'Affiliate code rewriting is active on PRO licenses. You can save your codes now and they will start working as soon as you upgrade.', 'my-book-showroom' ); ?>
						&nbsp;<a href="<?php echo esc_url( MBS_UPGRADE_URL ); ?>" target="_blank" rel="noopener noreferrer"><?php esc_html_e( 'Upgrade to PRO →', 'my-book-showroom' ); ?></a>
					</p>
				</div>
			<?php endif; ?>

			<div class="mbs-affiliate-tab-body<?php echo $is_pro ? '' : ' mbs-panel-disabled'; ?>"
			     style="<?php echo $is_pro ? '' : 'opacity:0.6;pointer-events:none;'; ?>">

				<div class="mbs-settings-card">
					<h3><?php esc_html_e( 'Retailer Affiliate Codes', 'my-book-showroom' ); ?></h3>

					<table class="form-table">
						<tr>
							<th><?php esc_html_e( 'Amazon Associates Tag', 'my-book-showroom' ); ?></th>
							<td>
								<input type="text" name="mbs_setting_affiliate_amazon"
								       value="<?php echo esc_attr( $amazon ); ?>"
								       class="regular-text" placeholder="your-tag-20">
								<p class="description"><?php esc_html_e( 'Appended as ?tag={CODE} to Amazon URLs.', 'my-book-showroom' ); ?></p>
							</td>
						</tr>
						<tr>
							<th><?php esc_html_e( 'Barnes & Noble / Rakuten', 'my-book-showroom' ); ?></th>
							<td>
								<input type="text" name="mbs_setting_affiliate_bn"
								       value="<?php echo esc_attr( $bn ); ?>"
								       class="regular-text" placeholder="sid123">
								<p class="description"><?php esc_html_e( 'Appended as ?sid={CODE} to Barnes & Noble URLs.', 'my-book-showroom' ); ?></p>
							</td>
						</tr>
						<tr>
							<th><?php esc_html_e( 'Kobo Affiliate', 'my-book-showroom' ); ?></th>
							<td>
								<input type="text" name="mbs_setting_affiliate_kobo"
								       value="<?php echo esc_attr( $kobo ); ?>"
								       class="regular-text" placeholder="your-utm-source">
								<p class="description"><?php esc_html_e( 'Appended as ?utm_source={CODE} to Kobo URLs.', 'my-book-showroom' ); ?></p>
							</td>
						</tr>
						<tr>
							<th><?php esc_html_e( 'Audible', 'my-book-showroom' ); ?></th>
							<td>
								<input type="text" name="mbs_setting_affiliate_audible"
								       value="<?php echo esc_attr( $audible ); ?>"
								       class="regular-text" placeholder="source-code">
								<p class="description"><?php esc_html_e( 'Appended as ?source_code={CODE} to Audible URLs.', 'my-book-showroom' ); ?></p>
							</td>
						</tr>
						<tr>
							<th><?php esc_html_e( 'Books-A-Million', 'my-book-showroom' ); ?></th>
							<td>
								<input type="text" name="mbs_setting_affiliate_bam"
								       value="<?php echo esc_attr( $bam ); ?>"
								       class="regular-text" placeholder="aff-id">
								<p class="description"><?php esc_html_e( 'Appended as ?aff={CODE} to BAM URLs.', 'my-book-showroom' ); ?></p>
							</td>
						</tr>
						<tr>
							<th><?php esc_html_e( 'Bookshop.org', 'my-book-showroom' ); ?></th>
							<td>
								<input type="text" name="mbs_setting_affiliate_bookshop"
								       value="<?php echo esc_attr( $bookshop ); ?>"
								       class="regular-text" placeholder="a_aid">
								<p class="description"><?php esc_html_e( 'Appended as ?a_aid={CODE} to Bookshop.org URLs.', 'my-book-showroom' ); ?></p>
							</td>
						</tr>
						<tr>
							<th><?php esc_html_e( 'Waterstones', 'my-book-showroom' ); ?></th>
							<td>
								<input type="text" name="mbs_setting_affiliate_waterstones"
								       value="<?php echo esc_attr( $waterstones ); ?>"
								       class="regular-text" placeholder="aff-id">
								<p class="description"><?php esc_html_e( 'Appended as ?aff={CODE} to Waterstones URLs.', 'my-book-showroom' ); ?></p>
							</td>
						</tr>
						<tr>
							<th><?php esc_html_e( 'Apple Books (Performance Partners)', 'my-book-showroom' ); ?></th>
							<td>
								<input type="text" name="mbs_setting_affiliate_apple"
								       value="<?php echo esc_attr( $apple ); ?>"
								       class="regular-text" placeholder="affiliate-token">
								<p class="description"><?php esc_html_e( 'Pass-through token appended server-side to Apple Books URLs (PRO).', 'my-book-showroom' ); ?></p>
							</td>
						</tr>
						<tr>
							<th><?php esc_html_e( 'Google Play (Affiliate Program)', 'my-book-showroom' ); ?></th>
							<td>
								<input type="text" name="mbs_setting_affiliate_google"
								       value="<?php echo esc_attr( $google ); ?>"
								       class="regular-text" placeholder="affiliate-token">
								<p class="description"><?php esc_html_e( 'Pass-through token appended server-side to Google Play URLs (PRO).', 'my-book-showroom' ); ?></p>
							</td>
						</tr>
						<tr>
							<th><?php esc_html_e( 'Powell\'s Books', 'my-book-showroom' ); ?></th>
							<td>
								<input type="text" name="mbs_setting_affiliate_powells"
								       value="<?php echo esc_attr( $powells ); ?>"
								       class="regular-text" placeholder="your-affiliate-id">
								<p class="description"><?php esc_html_e( 'Appended as ?affiliate={CODE} to Powell\'s Books URLs.', 'my-book-showroom' ); ?></p>
							</td>
						</tr>
					</table>
				</div>

				<div id="mbs-custom-stores-section" class="mbs-settings-card">
					<h3><?php esc_html_e( 'Custom Stores', 'my-book-showroom' ); ?><?php mbs_help_tip(
					__( 'A Custom Store is any retailer not in the standard list above — for example, your own website store, a regional retailer, or a direct-sales platform like Gumroad or Payhive. Custom stores appear alongside standard buy links on book pages. Unlike the standard retailers, custom stores do not support automatic affiliate code appending; the full buy URL for each book is entered manually in the book editor.', 'my-book-showroom' ),
					__( 'About Custom Stores', 'my-book-showroom' ),
					'mbs-help-tip--wide'
				); ?></h3>
					<div id="mbs-custom-stores-list"></div>
					<button type="button" id="mbs-add-custom-store" class="button">
						<?php esc_html_e( '+ Add Custom Store', 'my-book-showroom' ); ?>
					</button>
					<input type="hidden" name="mbs_setting_affiliate_custom_stores"
					       id="mbs-custom-stores-json"
					       value="<?php echo esc_attr( wp_json_encode( $custom_stores ) ); ?>">
				</div>

			</div><?php /* .mbs-affiliate-tab-body */ ?>

			<?php wp_nonce_field( 'mbs_affiliate_save', 'mbs_affiliate_nonce' ); ?>
			<?php submit_button( __( 'Save Affiliate Codes', 'my-book-showroom' ) ); ?>

		</div><?php /* .mbs-tab-affiliate */ ?>
	<?php
	}
}
