<?php
/**
 * Manager Widgets — the platform manager / owner view.
 *
 * Site-wide pulse: content totals, publishing trend, users, and (if present)
 * a WooCommerce snapshot. Reads only from cache supplied by the warmer /
 * orchestrator.
 *
 * @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 ManagerWidgets {

	public function __construct( private DataProviders $data ) {}

	public function render_pulse(): void {
		Renderer::print_css_once();

		$counts = $this->data->content_counts();
		$week   = $this->data->posts_published_in_last_days( 7 );
		$users  = $this->data->total_users();
		$new7   = $this->data->users_registered_in_last_days( 7 );

		echo Renderer::stat_row( [ // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
			[ 'label' => __( 'Posts', 'platform-helm' ),     'value' => number_format_i18n( $counts['post'] ?? 0 ) ],
			[ 'label' => __( 'Pages', 'platform-helm' ),     'value' => number_format_i18n( $counts['page'] ?? 0 ) ],
			[ 'label' => __( 'Published 7d', 'platform-helm' ),'value' => number_format_i18n( $week ) ],
			[ 'label' => __( 'Users', 'platform-helm' ),     'value' => number_format_i18n( $users ), 'sub' => sprintf( __( '+%s this week', 'platform-helm' ), number_format_i18n( $new7 ) ) ],
		] );

		echo '<div class="phelm-pd-section-title">' . esc_html__( 'Publishing — last 30 days', 'platform-helm' ) . '</div>';
		$series = $this->data->daily_publishing( 30 );
		$spark  = Renderer::sparkline( $series );
		echo $spark ? $spark : Renderer::empty_note( __( 'Not enough data yet.', 'platform-helm' ) ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped

		$woo = $this->data->woo_snapshot();
		if ( null !== $woo ) {
			echo '<div class="phelm-pd-section-title">' . esc_html__( 'Store — last 7 days', 'platform-helm' ) . '</div>';
			echo Renderer::stat_row( [ // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
				[ 'label' => __( 'Orders', 'platform-helm' ),  'value' => number_format_i18n( (int) $woo['orders_7d'] ) ],
				[ 'label' => __( 'Revenue', 'platform-helm' ), 'value' => number_format_i18n( (float) $woo['revenue_7d'], 0 ) ],
			] );
		}
	}

	public function render_recent_content(): void {
		Renderer::print_css_once();
		$posts = $this->data->recent_posts( 8 );
		if ( empty( $posts ) ) {
			echo Renderer::empty_note( __( 'No published posts yet.', 'platform-helm' ) ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
			return;
		}
		echo '<ul class="phelm-pd-list">';
		foreach ( $posts as $p ) {
			printf(
				'<li><span class="phelm-pd-li-main"><a href="%s">%s</a></span><span class="phelm-pd-li-meta">%s</span></li>',
				esc_url( (string) $p['edit_link'] ),
				esc_html( (string) $p['title'] ),
				esc_html( human_time_diff( (int) $p['date'], time() ) . ' ' . __( 'ago', 'platform-helm' ) )
			);
		}
		echo '</ul>';
	}
}
