<?php
/**
 * Community Widgets — the community manager's view.
 *
 * Engagement and moderation health, using comments as the universal community
 * signal (works on any site; membership/community plugins can extend via the
 * data filters). Shows activity volume, the moderation queue, and recent
 * comments to triage.
 *
 * @package Platform\Helm\Modules\Persona_Dashboards\Widgets
 */

declare( strict_types = 1 );

namespace Platform\Helm\Modules\Persona_Dashboards\Widgets;

use Platform\Helm\Modules\Persona_Dashboards\DataProviders;
use Platform\Helm\Modules\Persona_Dashboards\Renderer;

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

final class CommunityWidgets {

	public function __construct( private DataProviders $data ) {}

	public function render_pulse(): void {
		Renderer::print_css_once();
		echo Renderer::stat_row( [ // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
			[ 'label' => __( 'Comments 24h', 'platform-helm' ), 'value' => number_format_i18n( $this->data->comments_in_last_hours( 24 ) ) ],
			[ 'label' => __( 'Comments 7d', 'platform-helm' ),  'value' => number_format_i18n( $this->data->comments_in_last_hours( 24 * 7 ) ) ],
			[ 'label' => __( 'To moderate', 'platform-helm' ),  'value' => number_format_i18n( $this->data->pending_comments_count() ) ],
			[ 'label' => __( 'Spam', 'platform-helm' ),         'value' => number_format_i18n( $this->data->spam_comments_count() ) ],
		] );

		echo '<div class="phelm-pd-section-title">' . esc_html__( 'Recent comments', 'platform-helm' ) . '</div>';
		$recent = $this->data->recent_comments( 8 );
		if ( empty( $recent ) ) {
			echo Renderer::empty_note( __( 'No recent comments.', 'platform-helm' ) ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
			return;
		}
		echo '<ul class="phelm-pd-list">';
		foreach ( $recent as $c ) {
			printf(
				'<li><span class="phelm-pd-li-main"><a href="%s">%s</a>: %s</span><span class="phelm-pd-li-meta">%s</span></li>',
				esc_url( (string) $c['link'] ),
				esc_html( (string) $c['author'] ),
				esc_html( (string) $c['excerpt'] ),
				esc_html( human_time_diff( (int) $c['date'], time() ) )
			);
		}
		echo '</ul>';
	}
}
