<?php
/**
 * Dashboard partial: Store Trust Network status + early-warnings.
 *
 * Renders only when the network is active. Status figures come from data already
 * stored by the sync cron; the early-warning value is a count (never a dollar figure).
 *
 * @package TrustLens\Admin\Views
 * @since   1.3.6
 */

defined( 'ABSPATH' ) || exit;

if ( ! function_exists( 'wstl_network_is_active' ) || ! wstl_network_is_active() ) {
	return;
}

$wstl_net_status = (array) get_option( 'trustlens_network_sync_status', array() );
$wstl_net_counts = class_exists( 'TrustLens_Network_Flag_Repository' )
	? ( new TrustLens_Network_Flag_Repository() )->count_by_origin()
	: array(
		'local'  => 0,
		'remote' => 0,
	);
$wstl_net_peers  = class_exists( 'TrustLens_Network_Sync' )
	? TrustLens_Network_Sync::peer_urls( (string) get_option( 'trustlens_network_peers', '' ) )
	: array();
$wstl_net_peerst = isset( $wstl_net_status['peers'] ) && is_array( $wstl_net_status['peers'] ) ? $wstl_net_status['peers'] : array();
$wstl_net_ok     = 0;
foreach ( $wstl_net_peerst as $wstl_p ) {
	if ( is_array( $wstl_p ) && isset( $wstl_p['state'] ) && 'ok' === $wstl_p['state'] ) {
		++$wstl_net_ok;
	}
}
$wstl_net_last  = isset( $wstl_net_status['last_run'] ) ? (string) $wstl_net_status['last_run'] : '';
$wstl_net_warns = class_exists( 'TrustLens_Analytics' ) ? TrustLens_Analytics::get_network_early_warnings( 30 ) : 0;
?>
<div class="tlcc">
	<div class="wstl-card wstl-net-status">
		<h2><?php esc_html_e( 'Store Trust Network', 'trustlens' ); ?></h2>
		<div class="wstl-net-status-grid">
			<div>
				<b><?php echo esc_html( number_format_i18n( (int) $wstl_net_warns ) ); ?></b>
				<i><?php esc_html_e( 'early-warnings (30 days)', 'trustlens' ); ?></i>
			</div>
			<div>
				<b><?php echo esc_html( (string) $wstl_net_ok . '/' . count( $wstl_net_peers ) ); ?></b>
				<i><?php esc_html_e( 'peers healthy', 'trustlens' ); ?></i>
			</div>
			<div>
				<b><?php echo esc_html( number_format_i18n( (int) $wstl_net_counts['local'] ) ); ?></b>
				<i><?php esc_html_e( 'flags shared', 'trustlens' ); ?></i>
			</div>
			<div>
				<b><?php echo esc_html( number_format_i18n( (int) $wstl_net_counts['remote'] ) ); ?></b>
				<i><?php esc_html_e( 'flags pulled', 'trustlens' ); ?></i>
			</div>
		</div>
		<p class="wstl-net-status-foot">
			<?php esc_html_e( 'Shoppers a linked store flagged before they could build history here.', 'trustlens' ); ?>
			&middot;
			<?php esc_html_e( 'Last sync:', 'trustlens' ); ?>
			<?php
			echo '' !== $wstl_net_last
				/* translators: %s: human-readable time difference, e.g. "5 mins". */
				? esc_html( sprintf( __( '%s ago', 'trustlens' ), human_time_diff( (int) strtotime( $wstl_net_last ) ) ) )
				: esc_html__( 'never', 'trustlens' );
			?>
		</p>
		<style>
		.wstl-net-status-grid{display:flex;text-align:center;margin-top:6px}
		.wstl-net-status-grid div{flex:1;border-left:1px solid #eef1f4}
		.wstl-net-status-grid div:first-child{border-left:0}
		.wstl-net-status-grid b{display:block;font-size:20px;font-weight:800;color:#1e293b;font-variant-numeric:tabular-nums}
		.wstl-net-status-grid i{display:block;margin-top:2px;font-size:10px;font-style:normal;text-transform:uppercase;letter-spacing:.04em;color:#64748b}
		.wstl-net-status-foot{margin-top:11px;color:#64748b;font-size:12px}
		</style>
	</div>
</div>
