<?php
/**
 * Mode-mismatch admin notice (3.7.5).
 *
 * When Split Pay fails a transfer because a vendor's Stripe Connect account is in a
 * different mode (test vs live) than the API key used, TransferEngine fires the
 * `spp_transfer_mode_mismatch` action. We record the affected order(s) and surface a
 * prominent, dismissible admin banner telling the merchant to re-onboard the vendor in
 * the matching mode and retry — turning Stripe's cryptic "No such destination: a similar
 * object exists in test mode, but a live mode key was used" into a one-line self-serve
 * fix. (Production: Yuliana / levinarf.com — live store with test-mode vendor accounts.)
 *
 * @package BSD_Split_Pay_Stripe_Connect_Woo
 */

namespace BSD_Split_Pay_Stripe_Connect_Woo\Inc\Admin;

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

const SPP_MODE_MISMATCH_OPTION = 'bsd_spp_mode_mismatch_notices';

/**
 * Record a mode-mismatch transfer failure.
 *
 * Registered ALWAYS (the `spp_transfer_mode_mismatch` action fires during webhook / cron
 * transfer processing, not only in wp-admin), so the record survives to the next admin
 * page load. Deduped by order+account and capped to avoid unbounded option growth.
 *
 * @param array $ctx { order_id, destination, amount, error }.
 */
function spp_record_mode_mismatch( $ctx ): void {
	$order_id = isset( $ctx['order_id'] ) ? (int) $ctx['order_id'] : 0;
	$account  = isset( $ctx['destination'] ) ? (string) $ctx['destination'] : '';
	if ( $order_id <= 0 && '' === $account ) {
		return;
	}
	$notices = get_option( SPP_MODE_MISMATCH_OPTION, array() );
	if ( ! is_array( $notices ) ) {
		$notices = array();
	}
	$notices[ $order_id . '|' . $account ] = array(
		'order_id' => $order_id,
		'account'  => $account,
		'time'     => time(),
	);
	if ( count( $notices ) > 25 ) {
		$notices = array_slice( $notices, -25, null, true );
	}
	update_option( SPP_MODE_MISMATCH_OPTION, $notices, false );
}
add_action( 'spp_transfer_mode_mismatch', __NAMESPACE__ . '\\spp_record_mode_mismatch', 10, 1 );

/**
 * Render the dismissible admin banner while there are recorded mismatches. Dismissing it
 * (the X) deletes SPP_MODE_MISMATCH_OPTION via the shared persist-dismiss AJAX (the option
 * key is whitelisted in class-bsd-sca.php::persist_dismiss_notice), so it reappears only if
 * a NEW mismatch occurs.
 */
function spp_render_mode_mismatch_notice(): void {
	if ( ! current_user_can( 'manage_options' ) ) {
		return;
	}
	$notices = get_option( SPP_MODE_MISMATCH_OPTION, array() );
	if ( empty( $notices ) || ! is_array( $notices ) ) {
		return;
	}

	$order_ids = array();
	foreach ( $notices as $n ) {
		$oid = (int) ( $n['order_id'] ?? 0 );
		if ( $oid > 0 ) {
			$order_ids[ $oid ] = true;
		}
	}
	$order_ids = array_keys( $order_ids );

	$links = array();
	foreach ( array_slice( $order_ids, 0, 12 ) as $oid ) {
		$links[] = '<a href="' . esc_url( admin_url( 'post.php?post=' . $oid . '&action=edit' ) ) . '">#' . esc_html( (string) $oid ) . '</a>';
	}
	$onboard_url = admin_url( 'admin.php?page=bsd-split-pay-stripe-connect-woo-settings&tab=vendor-onboarding' );

	echo '<div class="notice notice-error is-dismissible" data-spp-persist-dismiss="' . esc_attr( SPP_MODE_MISMATCH_OPTION ) . '">';
	echo '<p><strong>' . esc_html__( 'Split Pay: vendor payouts blocked by a Stripe live/test mode mismatch', 'bsd-split-pay-stripe-connect-woo' ) . '</strong></p>';
	echo '<p>' . wp_kses_post(
		sprintf(
			/* translators: %s: link to the Vendor Onboarding settings tab. */
			__( 'One or more vendor Stripe Connect accounts are in a different mode (test vs live) than your store\'s Stripe API key, so Stripe refused the transfers — this is a configuration issue, not a failed payment. Re-onboard the affected vendor(s) from %s in your store\'s current mode, then use the &ldquo;Retry Split Pay Transfers&rdquo; order action.', 'bsd-split-pay-stripe-connect-woo' ),
			'<a href="' . esc_url( $onboard_url ) . '">' . esc_html__( 'Split Pay &rarr; Vendor Onboarding', 'bsd-split-pay-stripe-connect-woo' ) . '</a>'
		)
	) . '</p>';
	if ( ! empty( $links ) ) {
		echo '<p>' . esc_html__( 'Affected orders:', 'bsd-split-pay-stripe-connect-woo' ) . ' ' . wp_kses_post( implode( ', ', $links ) ) . '</p>';
	}
	echo '</div>';
}
add_action( 'admin_notices', __NAMESPACE__ . '\\spp_render_mode_mismatch_notice' );
