<?php
/**
 * Mini Progress Block
 *
 * A compact, understated progress bar for overview/listing contexts.
 * Left half shows the goal text, right half renders a slim bar.
 *
 * @package AmbassadorHub\Fundraisers\Blocks\MiniProgress
 */

namespace AmbassadorHub\Fundraisers\Blocks\MiniProgress;

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

use AmbassadorHub\Fundraisers\Admin\Settings\SettingsManager;
use AmbassadorHub\Fundraisers\Blocks\Stats\Block as StatsBlock;

final class Block {
	public static function init(): void {
		add_action( 'init', array( self::class, 'register_block' ) );
	}

	public static function register_block(): void {
		$handle    = 'ambh-mini-progress-block';
		$block_dir = AMBASSADOR_HUB_PATH . 'src/Fundraisers/Blocks/MiniProgress/';
		$block_url = AMBASSADOR_HUB_URL . 'src/Fundraisers/Blocks/MiniProgress/';

		$script_file = $block_dir . 'index.js';
		if ( file_exists( $script_file ) ) {
			wp_register_script(
				$handle,
				$block_url . 'index.js',
				array( 'wp-blocks', 'wp-i18n', 'wp-element', 'wp-server-side-render', 'wp-block-editor' ),
				filemtime( $script_file ),
				true
			);
		}

		wp_register_style(
			'ambh-mini-progress',
			$block_url . 'style.css',
			array(),
			file_exists( $block_dir . 'style.css' ) ? (string) filemtime( $block_dir . 'style.css' ) : AMBASSADOR_HUB_VERSION
		);

		$block_args = array(
			'style'           => 'ambh-mini-progress',
			'render_callback' => array( self::class, 'render_block' ),
		);

		if ( file_exists( $script_file ) ) {
			$block_args['editor_script'] = $handle;
		}

		register_block_type( $block_dir, $block_args );
	}

	public static function render_block( array $attributes = array() ): string {
		if ( defined( 'REST_REQUEST' ) && REST_REQUEST ) {
			return '<div style="padding: 12px; background: #f0f0f1; border: 1px solid #ddd; text-align: center; font-size: 0.85em;">'
				. esc_html__( 'Mini progress bar', 'ambassadorhub' )
				. '</div>';
		}

		$stats = StatsBlock::resolve_stats( $attributes );
		if ( array() === $stats || (float) $stats['target'] <= 0 ) {
			return '';
		}

		$progress      = min( (float) $stats['progress'], 100.0 );
		$wrapper_attrs = get_block_wrapper_attributes( array( 'class' => 'ambh-mini-progress' ) );

		ob_start();
		?>
		<div <?php echo $wrapper_attrs; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>>
			<span class="ambh-mini-progress__goal">
				<?php echo esc_html( SettingsManager::format_currency( $stats['total_raised'] ) ); ?>
				<span class="ambh-mini-progress__target">/ <?php echo esc_html( SettingsManager::format_currency( $stats['target'] ) ); ?></span>
			</span>
			<span class="ambh-mini-progress__bar" role="progressbar" aria-valuenow="<?php echo esc_attr( (string) round( $progress ) ); ?>" aria-valuemin="0" aria-valuemax="100" aria-label="<?php echo esc_attr( round( $progress ) . '%' ); ?>">
				<span class="ambh-mini-progress__fill" style="width: <?php echo esc_attr( $progress ); ?>%;"></span>
			</span>
		</div>
		<?php
		return (string) ob_get_clean();
	}
}
