<?php
/**
 * Renderer — shared CSS + HTML helpers for persona dashboard widgets.
 *
 * Widgets call print_css_once() at the top of render (guarded to emit once per
 * request), then use the stat_card / list / sparkline helpers so they stay
 * focused on data → markup.
 *
 * @package Platform\Helm\Modules\Persona_Dashboards
 */

declare( strict_types = 1 );

namespace Platform\Helm\Modules\Persona_Dashboards;

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

final class Renderer {

	private static bool $css_printed = false;

	public static function print_css_once(): void {
		if ( self::$css_printed ) {
			return;
		}
		self::$css_printed = true;
		echo self::css(); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
	}

	/**
	 * @param array<int,array{label:string,value:string,sub?:string}> $cards
	 */
	public static function stat_row( array $cards ): string {
		$html = '<div class="phelm-pd-stats">';
		foreach ( $cards as $c ) {
			$html .= '<div class="phelm-pd-stat">';
			$html .= '<div class="phelm-pd-stat-label">' . esc_html( (string) $c['label'] ) . '</div>';
			$html .= '<div class="phelm-pd-stat-value">' . esc_html( (string) $c['value'] ) . '</div>';
			if ( ! empty( $c['sub'] ) ) {
				$html .= '<div class="phelm-pd-stat-sub">' . esc_html( (string) $c['sub'] ) . '</div>';
			}
			$html .= '</div>';
		}
		$html .= '</div>';
		return $html;
	}

	/**
	 * Build an inline SVG sparkline from integers.
	 *
	 * @param int[] $values
	 */
	public static function sparkline( array $values, int $width = 240, int $height = 44, string $color = '#0047AB' ): string {
		$count = count( $values );
		if ( $count < 2 ) {
			return '';
		}
		$max   = max( $values );
		$min   = min( $values );
		$range = max( 1, $max - $min );
		$pad   = 2;
		$iw    = $width - $pad * 2;
		$ih    = $height - $pad * 2;

		$points = [];
		foreach ( $values as $i => $v ) {
			$x = $pad + ( $i / max( 1, $count - 1 ) ) * $iw;
			$y = $pad + $ih - ( ( $v - $min ) / $range ) * $ih;
			$points[] = sprintf( '%.1f,%.1f', $x, $y );
		}
		$poly = implode( ' ', $points );

		return sprintf(
			'<svg class="phelm-pd-spark" viewBox="0 0 %1$d %2$d" width="100%%" height="%2$d" preserveAspectRatio="none" role="img" aria-hidden="true"><polyline fill="none" stroke="%3$s" stroke-width="2" stroke-linejoin="round" stroke-linecap="round" points="%4$s"/></svg>',
			$width,
			$height,
			esc_attr( $color ),
			esc_attr( $poly )
		);
	}

	public static function empty_note( string $text ): string {
		return '<p class="phelm-pd-empty">' . esc_html( $text ) . '</p>';
	}

	public static function multi_role_note( array $personas ): string {
		if ( count( $personas ) < 2 ) {
			return '';
		}
		$labels = array_map( [ self::class, 'persona_label' ], $personas );
		return '<div class="phelm-pd-multirole">'
			. esc_html__( 'You hold multiple roles:', 'platform-helm' ) . ' '
			. esc_html( implode( ', ', $labels ) )
			. '</div>';
	}

	public static function persona_label( string $persona ): string {
		switch ( $persona ) {
			case PersonaDashboardsModule::PERSONA_MANAGER:   return __( 'Platform Manager', 'platform-helm' );
			case PersonaDashboardsModule::PERSONA_EDITOR:    return __( 'Editor', 'platform-helm' );
			case PersonaDashboardsModule::PERSONA_AUTHOR:    return __( 'Author', 'platform-helm' );
			case PersonaDashboardsModule::PERSONA_COMMUNITY: return __( 'Community Manager', 'platform-helm' );
			default:                                         return __( 'Member', 'platform-helm' );
		}
	}

	private static function css(): string {
		ob_start();
		?>
<style id="platform-helm-pd-styles">
.phelm-pd-stats{display:grid;grid-template-columns:repeat(auto-fit,minmax(110px,1fr));gap:10px;margin:4px 0 12px;}
.phelm-pd-stat{background:#f7f9fc;border:1px solid #e5eaf2;border-radius:8px;padding:10px 12px;}
.phelm-pd-stat-label{font-size:10px;font-weight:600;text-transform:uppercase;letter-spacing:.4px;color:#5c6b82;}
.phelm-pd-stat-value{font-size:22px;font-weight:700;color:#0047AB;line-height:1.1;margin-top:2px;font-variant-numeric:tabular-nums;}
.phelm-pd-stat-sub{font-size:11px;color:#5c6b82;margin-top:1px;}
.phelm-pd-list{list-style:none;margin:6px 0 0;padding:0;}
.phelm-pd-list li{display:flex;justify-content:space-between;gap:10px;padding:6px 0;border-bottom:1px solid #f0f2f6;font-size:12px;}
.phelm-pd-list li:last-child{border-bottom:none;}
.phelm-pd-list .phelm-pd-li-main{color:#0f1a33;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;}
.phelm-pd-list .phelm-pd-li-meta{color:#8a95a8;white-space:nowrap;flex-shrink:0;}
.phelm-pd-list a{text-decoration:none;color:#0047AB;}
.phelm-pd-empty{font-size:12px;color:#8a95a8;margin:8px 0 0;}
.phelm-pd-spark{display:block;margin:4px 0 10px;}
.phelm-pd-multirole{font-size:11px;color:#5c6b82;background:#eaf1fb;border:1px solid #c9d8ef;border-radius:6px;padding:6px 10px;margin:0 0 12px;}
.phelm-pd-section-title{font-size:11px;font-weight:600;text-transform:uppercase;letter-spacing:.4px;color:#5c6b82;margin:14px 0 4px;}
</style>
		<?php
		return (string) ob_get_clean();
	}
}
