<?php
/**
 * Dashboard module-status pill row.
 *
 * Compact at-a-glance status for features that otherwise have no dashboard
 * surface: Card-Testing Defense, Automation, Webhooks, Payment Controls,
 * Shipping Anomalies, Scheduled Reports. Each pill shows an on/off/Pro
 * state and one micro-stat, linking to the relevant admin page.
 *
 * @package TrustLens\Admin\Views\Partials
 * @since   1.2.2
 */

defined( 'ABSPATH' ) || exit;

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

$can_pro     = function_exists( 'wstl_can_use_pro' ) && wstl_can_use_pro();
$upgrade_url = function_exists( 'wstl_get_upgrade_or_fallback_url' ) ? wstl_get_upgrade_or_fallback_url() : '';

$pills = array();

// --- Card-Testing Defense (base) --------------------------------------
if ( class_exists( 'TrustLens_Admin_Card_Testing_Page' ) ) {
	$ct_enabled = (bool) get_option( 'trustlens_module_card_testing_enabled', false );
	$ct_state   = 'idle';
	$ct_label   = __( 'Idle', 'trustlens' );
	$ct_stat    = __( 'No attack', 'trustlens' );

	if ( $ct_enabled && class_exists( 'TrustLens_Lockdown_State' ) ) {
		$lockdown = new TrustLens_Lockdown_State();
		$current  = $lockdown->current_state();
		if ( 'PANIC' === $current ) {
			$ct_state = 'danger';
			$ct_label = __( 'Panic', 'trustlens' );
			$ct_stat  = __( 'All checkouts frozen', 'trustlens' );
		} elseif ( 'TARGETED' === $current ) {
			$ct_state = 'warn';
			$ct_label = __( 'Targeted', 'trustlens' );
			$ct_stat  = sprintf(
				/* translators: %d: number of fingerprints held. */
				_n( '%d held', '%d held', count( $lockdown->currently_targeted() ), 'trustlens' ),
				count( $lockdown->currently_targeted() )
			);
		} else {
			global $wpdb;
			// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Real-time dashboard pill count from a custom table; table name from $wpdb->prefix.
			$declines = (int) $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->prefix}trustlens_velocity_events WHERE event_type = 'decline' AND created_at > DATE_SUB(UTC_TIMESTAMP(), INTERVAL 24 HOUR)" );
			$ct_stat  = sprintf(
				/* translators: %d: 24h decline count. */
				_n( '%d decline · 24h', '%d declines · 24h', $declines, 'trustlens' ),
				$declines
			);
		}
	}

	$pills[] = array(
		'icon'   => 'shield-alt',
		'title'  => __( 'Card-Testing Defense', 'trustlens' ),
		'state'  => $ct_enabled ? $ct_state : 'off',
		'label'  => $ct_enabled ? $ct_label : __( 'Off', 'trustlens' ),
		'stat'   => $ct_enabled ? $ct_stat : __( 'Disabled in settings', 'trustlens' ),
		'href'   => admin_url( 'admin.php?page=' . TrustLens_Admin_Card_Testing_Page::PAGE_SLUG ),
		'is_pro' => false,
	);
}

// --- Automation (Pro) -------------------------------------------------
if ( wstl_has_automation() ) {
	if ( $can_pro ) {
		$rules   = get_option( 'trustlens_automation_rules', array() );
		$rules   = is_array( $rules ) ? $rules : array();
		$active  = 0;
		foreach ( $rules as $r ) {
			if ( ! empty( $r['enabled'] ) ) {
				$active++;
			}
		}
		$enabled = (bool) get_option( 'trustlens_enable_automation', false );
		$pills[] = array(
			'icon'   => 'admin-generic',
			'title'  => __( 'Automation', 'trustlens' ),
			'state'  => $enabled && $active > 0 ? 'good' : ( $enabled ? 'idle' : 'off' ),
			'label'  => $enabled ? __( 'On', 'trustlens' ) : __( 'Off', 'trustlens' ),
			'stat'   => sprintf(
				/* translators: %d: active rule count. */
				_n( '%d active rule', '%d active rules', $active, 'trustlens' ),
				$active
			),
			'href'   => admin_url( 'admin.php?page=trustlens-automation' ),
			'is_pro' => false,
		);
	} else {
		$pills[] = array(
			'icon'   => 'admin-generic',
			'title'  => __( 'Automation', 'trustlens' ),
			'state'  => 'pro',
			'label'  => __( 'Pro', 'trustlens' ),
			'stat'   => __( 'Rule-based actions on risky customers', 'trustlens' ),
			'href'   => $upgrade_url,
			'is_pro' => true,
		);
	}
}

// --- Webhooks (Pro) ---------------------------------------------------
if ( wstl_has_webhooks() ) {
	if ( $can_pro ) {
		$endpoints = get_option( 'trustlens_webhook_endpoints', array() );
		$endpoints = is_array( $endpoints ) ? $endpoints : array();
		$active    = 0;
		foreach ( $endpoints as $e ) {
			if ( ! empty( $e['enabled'] ) ) {
				$active++;
			}
		}
		$enabled = (bool) get_option( 'trustlens_enable_webhooks', false );
		$pills[] = array(
			'icon'   => 'rest-api',
			'title'  => __( 'Webhooks', 'trustlens' ),
			'state'  => $enabled && $active > 0 ? 'good' : ( $enabled ? 'idle' : 'off' ),
			'label'  => $enabled ? __( 'On', 'trustlens' ) : __( 'Off', 'trustlens' ),
			'stat'   => sprintf(
				/* translators: %d: active endpoint count. */
				_n( '%d endpoint', '%d endpoints', $active, 'trustlens' ),
				$active
			),
			'href'   => admin_url( 'admin.php?page=trustlens-settings&tab=webhooks' ),
			'is_pro' => false,
		);
	} else {
		$pills[] = array(
			'icon'   => 'rest-api',
			'title'  => __( 'Webhooks', 'trustlens' ),
			'state'  => 'pro',
			'label'  => __( 'Pro', 'trustlens' ),
			'stat'   => __( 'Send events to external services', 'trustlens' ),
			'href'   => $upgrade_url,
			'is_pro' => true,
		);
	}
}

// --- Payment Controls (Pro) -------------------------------------------
if ( wstl_has_payment_method_controls() ) {
	if ( $can_pro ) {
		$enabled = (bool) get_option( 'trustlens_enable_payment_method_controls', false );
		$gws     = get_option( 'trustlens_payment_method_control_gateways', array() );
		$gws     = is_array( $gws ) ? $gws : array();
		$pills[] = array(
			'icon'   => 'money-alt',
			'title'  => __( 'Payment Controls', 'trustlens' ),
			'state'  => $enabled && ! empty( $gws ) ? 'good' : ( $enabled ? 'idle' : 'off' ),
			'label'  => $enabled ? __( 'On', 'trustlens' ) : __( 'Off', 'trustlens' ),
			'stat'   => sprintf(
				/* translators: %d: controlled gateway count. */
				_n( '%d controlled gateway', '%d controlled gateways', count( $gws ), 'trustlens' ),
				count( $gws )
			),
			'href'   => admin_url( 'admin.php?page=trustlens-settings&tab=payment-controls' ),
			'is_pro' => false,
		);
	} else {
		$pills[] = array(
			'icon'   => 'money-alt',
			'title'  => __( 'Payment Controls', 'trustlens' ),
			'state'  => 'pro',
			'label'  => __( 'Pro', 'trustlens' ),
			'stat'   => __( 'Restrict risky methods per segment', 'trustlens' ),
			'href'   => $upgrade_url,
			'is_pro' => true,
		);
	}
}

// --- Shipping Anomalies (base) ----------------------------------------
$sa_enabled = (bool) get_option( 'trustlens_module_shipping_anomalies_enabled', true );
$pills[] = array(
	'icon'   => 'location-alt',
	'title'  => __( 'Shipping Anomalies', 'trustlens' ),
	'state'  => $sa_enabled ? 'good' : 'off',
	'label'  => $sa_enabled ? __( 'On', 'trustlens' ) : __( 'Off', 'trustlens' ),
	'stat'   => $sa_enabled ? __( 'Address-velocity + geo-diversity scoring', 'trustlens' ) : __( 'Disabled in settings', 'trustlens' ),
	'href'   => admin_url( 'admin.php?page=trustlens-settings&tab=modules' ),
	'is_pro' => false,
);

// --- Scheduled Reports (Pro) ------------------------------------------
if ( wstl_has_scheduled_reports() ) {
	if ( $can_pro ) {
		$enabled = (bool) get_option( 'trustlens_enable_scheduled_reports', false );
		$weekly  = (bool) get_option( 'trustlens_enable_weekly_reports', false );
		$monthly = (bool) get_option( 'trustlens_enable_monthly_reports', false );
		$parts   = array();
		if ( $weekly ) {
			$parts[] = __( 'weekly', 'trustlens' );
		}
		if ( $monthly ) {
			$parts[] = __( 'monthly', 'trustlens' );
		}
		$pills[] = array(
			'icon'   => 'email-alt',
			'title'  => __( 'Scheduled Reports', 'trustlens' ),
			'state'  => $enabled && ! empty( $parts ) ? 'good' : ( $enabled ? 'idle' : 'off' ),
			'label'  => $enabled ? __( 'On', 'trustlens' ) : __( 'Off', 'trustlens' ),
			'stat'   => ! empty( $parts ) ? implode( ' · ', $parts ) : __( 'No schedules configured', 'trustlens' ),
			'href'   => admin_url( 'admin.php?page=trustlens-settings&tab=reports' ),
			'is_pro' => false,
		);
	} else {
		$pills[] = array(
			'icon'   => 'email-alt',
			'title'  => __( 'Scheduled Reports', 'trustlens' ),
			'state'  => 'pro',
			'label'  => __( 'Pro', 'trustlens' ),
			'stat'   => __( 'Weekly and monthly email digests', 'trustlens' ),
			'href'   => $upgrade_url,
			'is_pro' => true,
		);
	}
}

if ( empty( $pills ) ) {
	return;
}
?>
<div class="wstl-dashboard-section">
	<div class="wstl-module-pills">
		<?php foreach ( $pills as $pill ) : ?>
			<a class="wstl-module-pill wstl-module-pill-<?php echo esc_attr( $pill['state'] ); ?><?php echo ! empty( $pill['is_pro'] ) ? ' is-upsell' : ''; ?>"
			   href="<?php echo esc_url( $pill['href'] ); ?>">
				<span class="dashicons dashicons-<?php echo esc_attr( $pill['icon'] ); ?> wstl-module-pill-icon" aria-hidden="true"></span>
				<div class="wstl-module-pill-body">
					<div class="wstl-module-pill-title"><?php echo esc_html( $pill['title'] ); ?></div>
					<div class="wstl-module-pill-meta">
						<span class="wstl-module-state"><?php echo esc_html( $pill['label'] ); ?></span>
						<span class="wstl-module-stat"><?php echo esc_html( $pill['stat'] ); ?></span>
					</div>
				</div>
			</a>
		<?php endforeach; ?>
	</div>
</div>
