<?php
/**
 * Author Widgets — the author's personal view.
 *
 * Scoped to the current user: their published/draft/pending counts, total
 * comments earned, and their most recently edited posts.
 *
 * @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 AuthorWidgets {

	public function __construct( private DataProviders $data ) {}

	public function render_my_content(): void {
		Renderer::print_css_once();
		$user_id = get_current_user_id();
		$stats   = $this->data->author_stats( $user_id );

		echo Renderer::stat_row( [ // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
			[ 'label' => __( 'Published', 'platform-helm' ), 'value' => number_format_i18n( $stats['published'] ) ],
			[ 'label' => __( 'Drafts', 'platform-helm' ),    'value' => number_format_i18n( $stats['drafts'] ) ],
			[ 'label' => __( 'Pending', 'platform-helm' ),   'value' => number_format_i18n( $stats['pending'] ) ],
			[ 'label' => __( 'Comments', 'platform-helm' ),  'value' => number_format_i18n( $stats['comments'] ) ],
		] );

		echo '<div class="phelm-pd-section-title">' . esc_html__( 'My recent posts', 'platform-helm' ) . '</div>';
		$recent = $this->data->author_recent_posts( $user_id, 6 );
		if ( empty( $recent ) ) {
			echo Renderer::empty_note( __( 'You have not written anything yet.', 'platform-helm' ) ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
			return;
		}
		echo '<ul class="phelm-pd-list">';
		foreach ( $recent 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( (string) $p['status'] )
			);
		}
		echo '</ul>';
	}
}
