<?php
/**
 * Dashboard Chargeback Ratio summary card.
 *
 * Shown to all users when the Chargebacks module is present. Everyone sees
 * the blended calendar-month ratio, a colored status dot, and an English
 * status label (Healthy / Approaching threshold / Action needed). Free gets
 * an upgrade CTA; Pro gets a "View details →" link to the dedicated
 * Chargeback Monitor page, which has the per-brand breakdown, trend chart,
 * proximity math, and email alerts.
 *
 * @package TrustLens
 * @since   1.2.1
 */

defined( 'ABSPATH' ) || exit;

// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound -- View template; variables are scoped to the including method, not the global namespace.

if ( ! class_exists( 'TrustLens_Chargeback_Monitor' ) || ! wstl_has_chargebacks() ) {
	return;
}

$monitor  = new TrustLens_Chargeback_Monitor();
$ratios   = $monitor->get_ratios();
$month    = $ratios['calendar_month'];
$nearest  = TrustLens_Chargeback_Monitor::get_nearest_threshold( $month );
$can_pro  = wstl_can_use_pro();
$page_url = admin_url( 'admin.php?page=trustlens-chargeback-monitor' );
$upsell   = function_exists( 'wstl_get_upgrade_or_fallback_url' ) ? wstl_get_upgrade_or_fallback_url() : '';

// Dot color + human-readable label. Derived from nearest-threshold state but
// intentionally does not expose the "N disputes from X%" math — that's Pro.
$dot_status   = 'healthy';
$status_label = __( 'Healthy', 'trustlens' );
if ( null === $nearest ) {
	$status_label = __( 'No card orders yet', 'trustlens' );
} elseif ( 'danger' === $nearest['status'] ) {
	$dot_status   = 'danger';
	$status_label = __( 'Action needed', 'trustlens' );
} elseif ( $nearest['disputes_to_hit'] > 0 && $nearest['disputes_to_hit'] <= 3 ) {
	$dot_status   = 'warning';
	$status_label = __( 'Approaching threshold', 'trustlens' );
}
?>

<div class="wstl-dashboard-section">
	<div class="wstl-card wstl-card-chargeback-ratio">
		<h2>
			<?php esc_html_e( 'Chargeback Ratio', 'trustlens' ); ?>
			<span class="wstl-dot wstl-dot-<?php echo esc_attr( $dot_status ); ?>" aria-hidden="true"></span>
		</h2>

		<div class="wstl-ratio-grid">
			<div class="wstl-ratio-cell">
				<div class="wstl-ratio-label"><?php esc_html_e( 'This month', 'trustlens' ); ?></div>
				<div class="wstl-ratio-value">
					<?php echo esc_html( number_format( 100 * (float) $month['blended']['ratio'], 2 ) ); ?>%
				</div>
				<div class="wstl-ratio-sub">
					<?php
					printf(
						/* translators: 1: disputes, 2: orders */
						esc_html__( '%1$d disputes / %2$d card orders', 'trustlens' ),
						(int) $month['blended']['disputes'],
						(int) $month['blended']['orders']
					);
					?>
				</div>
			</div>

			<div class="wstl-ratio-cell">
				<div class="wstl-ratio-label"><?php esc_html_e( 'Status', 'trustlens' ); ?></div>
				<div class="wstl-ratio-value wstl-ratio-value-small wstl-ratio-status-<?php echo esc_attr( $dot_status ); ?>">
					<?php echo esc_html( $status_label ); ?>
				</div>
			</div>
		</div>

		<p>
			<?php if ( $can_pro ) : ?>
				<a href="<?php echo esc_url( $page_url ); ?>" class="button button-secondary">
					<?php esc_html_e( 'View details →', 'trustlens' ); ?>
				</a>
			<?php elseif ( wstl_show_pro_upsells() && $upsell ) : ?>
				<a href="<?php echo esc_url( $upsell ); ?>" class="button button-primary">
					<?php esc_html_e( 'Unlock per-brand ratios + alerts', 'trustlens' ); ?>
				</a>
				<span class="wstl-ratio-cta-note">
					<?php esc_html_e( 'Pro shows which brand is driving the ratio, how close you are to Visa / Mastercard / Amex thresholds, and emails you before you cross one.', 'trustlens' ); ?>
				</span>
			<?php endif; ?>
		</p>
	</div>
</div>
