<?php
/**
 * Modules settings tab — one scannable card per detection module.
 *
 * Each card carries the module icon, name, description, a brand toggle (the
 * live enable state), and any inline config. Pro sub-features appear inside the
 * relevant card. Card-Testing is controlled on its own page, so it shows status
 * + a link instead of a toggle.
 *
 * @package TrustLens\Admin\Views\Settings
 * @since   1.0.0
 */

defined( 'ABSPATH' ) || exit;

// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound -- Template-scoped helpers.

/** Build a stroke SVG icon from inner paths. */
$wstl_mod_icon = static function ( string $inner ): string {
	return '<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.7" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">' . $inner . '</svg>';
};

/** Render one module card with a brand toggle + optional inline config. */
$wstl_mod_card = static function ( string $icon, string $name, string $desc, string $field, bool $default, string $config = '' ): void {
	$on = (bool) get_option( $field, $default );
	?>
	<div class="wstl-card wstl-module-card">
		<div class="wstl-module-top">
			<span class="wstl-module-icon"><?php echo $icon; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Static SVG markup. ?></span>
			<div class="wstl-module-meta">
				<h3 class="wstl-module-name"><?php echo esc_html( $name ); ?></h3>
				<p class="wstl-module-desc"><?php echo esc_html( $desc ); ?></p>
			</div>
			<label class="wstl-toggle">
				<input type="hidden" name="<?php echo esc_attr( $field ); ?>" value="0">
				<input type="checkbox" class="wstl-module-toggle" name="<?php echo esc_attr( $field ); ?>" value="1" <?php checked( $on ); ?>>
				<span class="wstl-toggle-slider"></span>
			</label>
		</div>
		<?php if ( '' !== $config ) : ?>
			<div class="wstl-module-config"><?php echo $config; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Built from escaped parts below. ?></div>
		<?php endif; ?>
	</div>
	<?php
};

// --- Coupon config: max first-order + Pro linked-abuse ----------------------
ob_start();
?>
<div class="wstl-module-config-row">
	<label for="trustlens_coupons_max_first_order"><?php esc_html_e( 'Max first-order coupons', 'trustlens' ); ?></label>
	<input type="number" id="trustlens_coupons_max_first_order" name="trustlens_coupons_max_first_order" value="<?php echo esc_attr( get_option( 'trustlens_coupons_max_first_order', 2 ) ); ?>" min="1" max="20" step="1" class="small-text">
	<span class="wstl-module-config-hint"><?php esc_html_e( 'before flagging abuse', 'trustlens' ); ?></span>
</div>
<?php if ( wstl_can_use_pro() ) : ?>
	<div class="wstl-module-subrow">
		<span class="wstl-module-subrow-label"><?php esc_html_e( 'Block checkout when a linked account has used first-order coupons', 'trustlens' ); ?></span>
		<label class="wstl-toggle">
			<input type="hidden" name="trustlens_coupons_block_linked_abuse" value="0">
			<input type="checkbox" name="trustlens_coupons_block_linked_abuse" value="1" <?php checked( get_option( 'trustlens_coupons_block_linked_abuse', false ) ); ?>>
			<span class="wstl-toggle-slider"></span>
		</label>
	</div>
<?php elseif ( wstl_show_pro_upsells() ) : ?>
	<p class="wstl-module-pro-line">
		<?php esc_html_e( 'Block checkout when a linked account has used first-order coupons.', 'trustlens' ); ?>
		<?php echo wp_kses_post( wstl_pro_badge( true ) ); ?>
	</p>
<?php endif;
$wstl_coupon_config = ob_get_clean();

// --- Shipping config: velocity window + Pro trend analysis ------------------
ob_start();
?>
<div class="wstl-module-config-row">
	<label for="trustlens_shipping_anomalies_velocity_days"><?php esc_html_e( 'Velocity window', 'trustlens' ); ?></label>
	<input type="number" id="trustlens_shipping_anomalies_velocity_days" name="trustlens_shipping_anomalies_velocity_days" value="<?php echo esc_attr( get_option( 'trustlens_shipping_anomalies_velocity_days', 30 ) ); ?>" min="7" max="90" step="1" class="small-text">
	<span class="wstl-module-config-hint"><?php esc_html_e( 'days for rapid-change detection', 'trustlens' ); ?></span>
</div>
<?php if ( wstl_can_use_pro() ) : ?>
	<div class="wstl-module-subrow">
		<span class="wstl-module-subrow-label"><?php esc_html_e( 'Detect sudden behavioral shifts in address patterns', 'trustlens' ); ?></span>
		<label class="wstl-toggle">
			<input type="hidden" name="trustlens_shipping_anomalies_trend_enabled" value="0">
			<input type="checkbox" name="trustlens_shipping_anomalies_trend_enabled" value="1" <?php checked( get_option( 'trustlens_shipping_anomalies_trend_enabled', true ) ); ?>>
			<span class="wstl-toggle-slider"></span>
		</label>
	</div>
<?php elseif ( wstl_show_pro_upsells() ) : ?>
	<p class="wstl-module-pro-line">
		<?php esc_html_e( 'Detect sudden behavioral shifts in address patterns.', 'trustlens' ); ?>
		<?php echo wp_kses_post( wstl_pro_badge( true ) ); ?>
	</p>
<?php endif;
$wstl_shipping_config = ob_get_clean();

// --- Linked accounts config: penalty ----------------------------------------
ob_start();
?>
<div class="wstl-module-config-row">
	<label for="trustlens_linked_accounts_penalty"><?php esc_html_e( 'Link penalty', 'trustlens' ); ?></label>
	<input type="number" id="trustlens_linked_accounts_penalty" name="trustlens_linked_accounts_penalty" value="<?php echo esc_attr( get_option( 'trustlens_linked_accounts_penalty', 5 ) ); ?>" min="0" max="20" step="1" class="small-text">
	<span class="wstl-module-config-hint"><?php esc_html_e( 'points per high-risk linked account', 'trustlens' ); ?></span>
</div>
<?php
$wstl_linked_config = ob_get_clean();

// --- Chargebacks config: link to the dedicated Pro monitor page --------------
$wstl_chargebacks_config = '';
if ( wstl_has_chargebacks() ) {
	ob_start();
	?>
	<div class="wstl-module-config-row wstl-module-config-link">
		<a href="<?php echo esc_url( admin_url( 'admin.php?page=trustlens-chargeback-monitor' ) ); ?>" class="button button-secondary">
			<?php esc_html_e( 'Open Chargeback Monitor', 'trustlens' ); ?> &rarr;
		</a>
	</div>
	<?php
	$wstl_chargebacks_config = ob_get_clean();
}

$wstl_ct_on = (bool) get_option( 'trustlens_module_card_testing_enabled', false );
?>

<form method="post" action="options.php">
	<?php settings_fields( 'trustlens_modules' ); ?>

	<div class="wstl-module-grid">

	<?php
	$wstl_mod_card(
		$wstl_mod_icon( '<polyline points="1 4 1 10 7 10"/><path d="M3.51 15a9 9 0 1 0 2.13-9.36L1 10"/>' ),
		__( 'Return Abuse Detection', 'trustlens' ),
		__( 'Analyzes refund rates, values, and return patterns to flag serial returners.', 'trustlens' ),
		'trustlens_module_returns_enabled',
		true
	);

	$wstl_mod_card(
		$wstl_mod_icon( '<path d="M6 2 3 6v14a2 2 0 0 0 2 2h14a2 2 0 0 0 2-2V6l-3-4z"/><line x1="3" y1="6" x2="21" y2="6"/><path d="M16 10a4 4 0 0 1-8 0"/>' ),
		__( 'Order Tracking', 'trustlens' ),
		__( 'Monitors completion rates, cancellations, and order frequency.', 'trustlens' ),
		'trustlens_module_orders_enabled',
		true
	);

	$wstl_mod_card(
		$wstl_mod_icon( '<path d="M20.59 13.41 13.42 20.58a2 2 0 0 1-2.83 0L2 12V2h10l8.59 8.59a2 2 0 0 1 0 2.82z"/><line x1="7" y1="7" x2="7.01" y2="7"/>' ),
		__( 'Coupon Abuse Detection', 'trustlens' ),
		__( 'Identifies repeated first-order-discount abuse and refunded coupon orders.', 'trustlens' ),
		'trustlens_module_coupons_enabled',
		true,
		$wstl_coupon_config
	);

	$wstl_mod_card(
		$wstl_mod_icon( '<rect x="3" y="3" width="7" height="7" rx="1"/><rect x="14" y="3" width="7" height="7" rx="1"/><rect x="14" y="14" width="7" height="7" rx="1"/><rect x="3" y="14" width="7" height="7" rx="1"/>' ),
		__( 'Category-Aware Scoring', 'trustlens' ),
		__( 'Applies heavier return penalties for high-risk product categories.', 'trustlens' ),
		'trustlens_module_categories_enabled',
		true
	);

	$wstl_mod_card(
		$wstl_mod_icon( '<rect x="1" y="3" width="15" height="13" rx="1"/><path d="M16 8h4l3 3v5h-7V8z"/><circle cx="5.5" cy="18.5" r="2.5"/><circle cx="18.5" cy="18.5" r="2.5"/>' ),
		__( 'Shipping Address Anomalies', 'trustlens' ),
		__( 'Flags address hopping, country mismatches, and rapid address changes.', 'trustlens' ),
		'trustlens_module_shipping_anomalies_enabled',
		true,
		$wstl_shipping_config
	);

	$wstl_mod_card(
		$wstl_mod_icon( '<path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"/><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"/>' ),
		__( 'Linked Accounts Detection', 'trustlens' ),
		__( 'Uses device fingerprints and IPs to spot one person using multiple accounts.', 'trustlens' ),
		'trustlens_module_linked_accounts_enabled',
		true,
		$wstl_linked_config
	);

	if ( wstl_has_chargebacks() ) {
		$wstl_mod_card(
			$wstl_mod_icon( '<rect x="1" y="4" width="22" height="16" rx="2"/><line x1="1" y1="10" x2="23" y2="10"/>' ),
			__( 'Chargeback Tracking', 'trustlens' ),
			__( 'Records disputes from Stripe, WooPayments, or manual entry, and factors them into trust scores.', 'trustlens' ),
			'trustlens_module_chargebacks_enabled',
			true,
			$wstl_chargebacks_config
		);
	}
	?>

	<!-- Card-Testing Defense — the 8th module; controlled on its own page. -->
	<div class="wstl-card wstl-module-card wstl-module-card-link">
		<div class="wstl-module-top">
			<span class="wstl-module-icon"><?php echo $wstl_mod_icon( '<path d="M12 22s8-4 8-10V5l-8-3-8 3v7c0 6 8 10 8 10z"/>' ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Static SVG. ?></span>
			<div class="wstl-module-meta">
				<h3 class="wstl-module-name"><?php esc_html_e( 'Card-Testing Defense', 'trustlens' ); ?></h3>
				<p class="wstl-module-desc"><?php esc_html_e( 'Blocks automated stolen-card testing at checkout. Velocity thresholds and Panic Freeze live on its own page.', 'trustlens' ); ?></p>
			</div>
			<span class="wstl-module-status <?php echo $wstl_ct_on ? 'is-on' : 'is-off'; ?>">
				<?php echo $wstl_ct_on ? esc_html__( 'On', 'trustlens' ) : esc_html__( 'Off', 'trustlens' ); ?>
			</span>
		</div>
		<div class="wstl-module-config">
			<a href="<?php echo esc_url( admin_url( 'admin.php?page=trustlens-card-testing' ) ); ?>" class="button button-secondary">
				<?php esc_html_e( 'Open Card-Testing Defense', 'trustlens' ); ?> &rarr;
			</a>
		</div>
	</div>
	<?php
	// Store Trust Network — its own tab (mirrors Card-Testing's status+link card). Shown to
	// eligible installs with On/Off status, and as a discovery upsell to free users with a
	// Pro badge + "Learn more" (gated by wstl_show_pro_upsells(), same as other module upsells).
	$wstl_net_available = function_exists( 'wstl_can_use_network' ) && wstl_can_use_network();
	$wstl_net_promo     = ! $wstl_net_available && function_exists( 'wstl_show_pro_upsells' ) && wstl_show_pro_upsells();
	if ( $wstl_net_available || $wstl_net_promo ) :
		$wstl_net_on = $wstl_net_available
			&& (bool) get_option( 'trustlens_module_store_network_enabled', false )
			&& '' !== wstl_network_get_group_key();
		?>
		<div class="wstl-card wstl-module-card wstl-module-card-link">
			<div class="wstl-module-top">
				<span class="wstl-module-icon"><?php echo $wstl_mod_icon( '<circle cx="5" cy="6" r="2"/><circle cx="19" cy="6" r="2"/><circle cx="5" cy="18" r="2"/><circle cx="19" cy="18" r="2"/><circle cx="12" cy="12" r="2.4"/><line x1="6.7" y1="7.2" x2="10.1" y2="10.6"/><line x1="17.3" y1="7.2" x2="13.9" y2="10.6"/><line x1="6.7" y1="16.8" x2="10.1" y2="13.4"/><line x1="17.3" y1="16.8" x2="13.9" y2="13.4"/>' ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Static SVG markup. ?></span>
				<div class="wstl-module-meta">
					<h3 class="wstl-module-name">
						<?php esc_html_e( 'Store Trust Network', 'trustlens' ); ?>
						<?php echo $wstl_net_promo ? ' ' . wp_kses_post( wstl_pro_badge( false ) ) : ''; ?>
					</h3>
					<p class="wstl-module-desc"><?php esc_html_e( 'Shares customer risk flags across the stores you own. Configured on its own page.', 'trustlens' ); ?></p>
				</div>
				<?php if ( $wstl_net_available ) : ?>
					<span class="wstl-module-status <?php echo $wstl_net_on ? 'is-on' : 'is-off'; ?>">
						<?php echo $wstl_net_on ? esc_html__( 'On', 'trustlens' ) : esc_html__( 'Off', 'trustlens' ); ?>
					</span>
				<?php endif; ?>
			</div>
			<div class="wstl-module-config">
				<a href="<?php echo esc_url( admin_url( 'admin.php?page=trustlens-settings&tab=store-network' ) ); ?>" class="button button-secondary">
					<?php echo $wstl_net_available ? esc_html__( 'Open Store Trust Network', 'trustlens' ) : esc_html__( 'Learn more', 'trustlens' ); ?> &rarr;
				</a>
			</div>
		</div>
		<?php
	endif;
	?>
	</div><!-- /.wstl-module-grid -->

	<?php submit_button(); ?>
</form>

<?php // phpcs:enable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound ?>
