<?php
/**
 * Fundraising Progress Block
 *
 * @package AmbassadorHub\Fundraisers\Blocks\FundraisingProgress
 */

namespace AmbassadorHub\Fundraisers\Blocks\FundraisingProgress;

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

		$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-fundraising-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-fundraising-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__( '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-fundraising-progress-block' ) );

		ob_start();
		?>
		<div <?php echo $wrapper_attrs; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- sanitized by get_block_wrapper_attributes(). ?>>
			<div class="ambh-fundraising-progress-block__header">
				<span class="ambh-fundraising-progress-block__label"><?php echo ! empty( $stats['closed'] ) ? esc_html__( 'Result', 'ambassadorhub' ) : esc_html__( 'Progress', 'ambassadorhub' ); ?></span>
				<span class="ambh-fundraising-progress-block__percent"><?php echo esc_html( number_format( $stats['progress'], 1, $format['decimal'], $format['thousands'] ) ); ?>%</span>
			</div>
			<div class="ambh-fundraising-progress-block__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 ) . '%' ); ?>">
				<div class="ambh-fundraising-progress-block__fill" style="width: <?php echo esc_attr( $progress ); ?>%;"></div>
			</div>
			<div class="ambh-fundraising-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();
	}
}