<?php
/**
 * Circular Progress Block
 *
 * @package AmbassadorHub\Fundraisers\Blocks\CircularProgress
 */

namespace AmbassadorHub\Fundraisers\Blocks\CircularProgress;

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-circular-progress-block';
		$block_dir = AMBASSADOR_HUB_PATH . 'src/Fundraisers/Blocks/CircularProgress/';
		$block_url = AMBASSADOR_HUB_URL . 'src/Fundraisers/Blocks/CircularProgress/';

		$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-circular-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-circular-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: 20px; background: #f0f0f1; border: 1px solid #ddd; text-align: center;">'
				. esc_html__( 'Circular fundraising progress.', 'ambassadorhub' )
				. '</div>';
		}

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

		$format        = SettingsManager::get_currency_format();
		$progress      = min( (float) $stats['progress'], 100.0 );
		$wrapper_attrs = get_block_wrapper_attributes(
			array(
				'class' => 'ambh-circular-progress-block',
				'style' => '--ambh-progress-angle:' . esc_attr( (string) ( $progress * 3.6 ) ) . 'deg',
			)
		);

		ob_start();
		?>
		<div <?php echo $wrapper_attrs; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- sanitized by get_block_wrapper_attributes(). ?>>
			<div class="ambh-circular-progress-block__chart" 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 ) . '%' ); ?>">
				<div>
					<div class="ambh-circular-progress-block__value"><?php echo esc_html( number_format( $stats['progress'], 1, $format['decimal'], $format['thousands'] ) ); ?>%</div>
					<div class="ambh-circular-progress-block__subtext"><?php echo ! empty( $stats['closed'] ) ? esc_html__( 'Result', 'ambassadorhub' ) : esc_html__( 'Progress', 'ambassadorhub' ); ?></div>
				</div>
			</div>
			<div class="ambh-circular-progress-block__label"><?php esc_html_e( 'Fundraising progress', 'ambassadorhub' ); ?></div>
			<div class="ambh-circular-progress-block__footer"><?php echo esc_html( SettingsManager::format_currency( $stats['total_raised'] ) ); ?> <?php esc_html_e( 'of', 'ambassadorhub' ); ?> <?php echo esc_html( SettingsManager::format_currency( $stats['target'] ) ); ?></div>
		</div>
		<?php
		return (string) ob_get_clean();
	}
}