<?php
/**
 * Accounts Admin Widgets
 *
 * Registers dashboard widgets for ambassador statistics.
 *
 * @package AmbassadorHub\Accounts\Admin
 */

namespace AmbassadorHub\Accounts\Admin;

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

use AmbassadorHub\Accounts\Helpers;
use AmbassadorHub\Admin\InlineAssets;

use function add_action;
use function wp_add_dashboard_widget;
use function esc_html__;
use function current_user_can;
use function esc_html;
use function get_current_screen;

/**
 * Registers dashboard widgets for the Accounts module.
 */
final class Widgets {
	/**
	 * Initialize widget hooks.
	 *
	 * @return void
	 */
	public static function init(): void {
		add_action( 'ambh_register_dashboard_widgets', array( self::class, 'register_widgets' ) );
		add_action( 'admin_enqueue_scripts', array( self::class, 'enqueue_assets' ) );
	}

	/**
	 * Enqueue the dashboard widget's inline CSS.
	 *
	 * Hooked to admin_enqueue_scripts, which fires early enough (before
	 * admin_print_styles) for wp_add_inline_style() to be picked up, unlike
	 * calling InlineAssets::style() from render_ambassador_stats() itself
	 * (which only runs once the dashboard widget boxes are being displayed,
	 * after the <head> has already printed).
	 *
	 * @return void
	 */
	public static function enqueue_assets(): void {
		$screen = get_current_screen();
		if ( ! $screen || 'dashboard' !== $screen->id ) {
			return;
		}

		if ( ! current_user_can( 'ambh_manage_hub' ) ) {
			return;
		}

		InlineAssets::style(
			'.ambassador-stats-widget .stats-grid {
				display: grid;
				grid-template-columns: 1fr 1fr;
				gap: 20px;
				margin-top: 15px;
			}
			.ambassador-stats-widget .stat-item {
				text-align: center;
				padding: 15px;
				background: #f5f5f5;
				border-radius: 4px;
			}
			.ambassador-stats-widget .stat-number {
				font-size: 32px;
				font-weight: bold;
				color: #0073aa;
				margin-bottom: 5px;
			}
			.ambassador-stats-widget .stat-label {
				font-size: 13px;
				color: #666;
			}'
		);
	}

	/**
	 * Register dashboard widgets.
	 *
	 * @return void
	 */
	public static function register_widgets(): void {
		if ( ! current_user_can( 'ambh_manage_hub' ) ) {
			return;
		}

		wp_add_dashboard_widget(
			'ambh_ambassador_stats',
			esc_html__( 'Ambassador Statistics', 'ambassadorhub' ),
			array( self::class, 'render_ambassador_stats' )
		);
	}

	/**
	 * Render the ambassador statistics widget.
	 *
	 * @return void
	 */
	public static function render_ambassador_stats(): void {
		// Get total number of ambassadors.
		$total_ambassadors = Helpers::get_total_ambassadors();

		// Get ambassadors registered in the past 7 days.
		$recent_ambassadors = Helpers::get_recent_ambassadors( 7 );

		?>
		<div class="ambassador-stats-widget">
			<div class="stats-grid">
				<div class="stat-item">
					<div class="stat-number"><?php echo esc_html( $total_ambassadors ); ?></div>
					<div class="stat-label"><?php esc_html_e( 'Total Ambassadors', 'ambassadorhub' ); ?></div>
				</div>
				<div class="stat-item">
					<div class="stat-number"><?php echo esc_html( $recent_ambassadors ); ?></div>
					<div class="stat-label"><?php esc_html_e( 'Registered (Last 7 Days)', 'ambassadorhub' ); ?></div>
				</div>
			</div>
		</div>
		<?php
	}
}
