<?php
/**
 * SVG donut score ring partial.
 *
 * @package MonitorCat
 *
 * @var int    $score Score value 0-100.
 * @var string $size  Ring size: 'sm' or 'md'. Default 'sm'.
 * @var string $color Stroke color. Default uses primary.
 */

defined( 'ABSPATH' ) || exit;

$monitorcat_ring_score = isset( $score ) ? max( 0, min( 100, (int) $score ) ) : 0;
$monitorcat_ring_size  = isset( $size ) ? $size : 'sm';
$monitorcat_ring_color = isset( $color ) ? $color : 'var(--mc-primary)';

$monitorcat_dimensions = 'md' === $monitorcat_ring_size ? 64 : 40;
$monitorcat_radius     = 'md' === $monitorcat_ring_size ? 26 : 16;
$monitorcat_stroke     = 'md' === $monitorcat_ring_size ? 6 : 4;
$monitorcat_circumf    = 2 * M_PI * $monitorcat_radius;
$monitorcat_dash_val   = ( $monitorcat_ring_score / 100 ) * $monitorcat_circumf;
$monitorcat_font_size  = 'md' === $monitorcat_ring_size ? '14px' : '11px';
?>

<div class="mc-score-ring mc-score-ring-<?php echo esc_attr( $monitorcat_ring_size ); ?>">
	<svg
		width="<?php echo esc_attr( (string) $monitorcat_dimensions ); ?>"
		height="<?php echo esc_attr( (string) $monitorcat_dimensions ); ?>"
		viewBox="0 0 <?php echo esc_attr( (string) $monitorcat_dimensions ); ?> <?php echo esc_attr( (string) $monitorcat_dimensions ); ?>"
		<?php /* translators: %d: numeric score value */ ?>
		aria-label="<?php echo esc_attr( sprintf( __( 'Score: %d out of 100', 'monitor-cat' ), $monitorcat_ring_score ) ); ?>"
		role="img"
	>
		<!-- Background track -->
		<circle
			cx="<?php echo esc_attr( (string) ( $monitorcat_dimensions / 2 ) ); ?>"
			cy="<?php echo esc_attr( (string) ( $monitorcat_dimensions / 2 ) ); ?>"
			r="<?php echo esc_attr( (string) $monitorcat_radius ); ?>"
			fill="none"
			stroke="var(--mc-border-color)"
			stroke-width="<?php echo esc_attr( (string) $monitorcat_stroke ); ?>"
		/>
		<!-- Progress arc -->
		<circle
			cx="<?php echo esc_attr( (string) ( $monitorcat_dimensions / 2 ) ); ?>"
			cy="<?php echo esc_attr( (string) ( $monitorcat_dimensions / 2 ) ); ?>"
			r="<?php echo esc_attr( (string) $monitorcat_radius ); ?>"
			fill="none"
			stroke="<?php echo esc_attr( $monitorcat_ring_color ); ?>"
			stroke-width="<?php echo esc_attr( (string) $monitorcat_stroke ); ?>"
			stroke-dasharray="<?php echo esc_attr( $monitorcat_dash_val . ' ' . $monitorcat_circumf ); ?>"
			stroke-dashoffset="0"
			stroke-linecap="round"
			transform="rotate(-90 <?php echo esc_attr( (string) ( $monitorcat_dimensions / 2 ) ); ?> <?php echo esc_attr( (string) ( $monitorcat_dimensions / 2 ) ); ?>)"
		/>
		<!-- Score text -->
		<text
			x="50%"
			y="50%"
			text-anchor="middle"
			dominant-baseline="central"
			fill="var(--mc-text)"
			font-size="<?php echo esc_attr( $monitorcat_font_size ); ?>"
			font-family="var(--mc-font-heading)"
			font-weight="600"
		><?php echo esc_html( (string) $monitorcat_ring_score ); ?></text>
	</svg>
</div>
