<?php
/**
 * Days Remaining Block
 *
 * @package AmbassadorHub\Fundraisers\Blocks\DaysRemaining
 */

namespace AmbassadorHub\Fundraisers\Blocks\DaysRemaining;

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

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-days-remaining-block';
		$block_dir = AMBASSADOR_HUB_PATH . 'src/Fundraisers/Blocks/DaysRemaining/';
		$block_url = AMBASSADOR_HUB_URL . 'src/Fundraisers/Blocks/DaysRemaining/';

		$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-days-remaining',
			$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-days-remaining',
			'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__( 'Days remaining.', 'ambassadorhub' )
				. '</div>';
		}

		$stats = StatsBlock::resolve_stats( $attributes );
		if ( array() === $stats ) {
			return '';
		}

		$days_remaining = $stats['days_remaining'] ?? null;
		$classes        = 'ambh-days-remaining-block';

		if ( 0 === $days_remaining ) {
			$classes .= ' is-closed';
			$value    = esc_html__( '0', 'ambassadorhub' );
			$label    = esc_html__( 'Closed', 'ambassadorhub' );
			$caption  = esc_html__( 'This fundraiser is no longer running', 'ambassadorhub' );
		} elseif ( null === $days_remaining ) {
			$value   = '—';
			$label   = esc_html__( 'Open ended', 'ambassadorhub' );
			$caption = esc_html__( 'No end date set', 'ambassadorhub' );
		} else {
			$value   = (string) $days_remaining;
			$label   = 1 === (int) $days_remaining ? esc_html__( 'Day left', 'ambassadorhub' ) : esc_html__( 'Days left', 'ambassadorhub' );
			$caption = esc_html__( 'Remaining in this fundraising context', 'ambassadorhub' );
		}

		$wrapper_attrs = get_block_wrapper_attributes( array( 'class' => $classes ) );

		ob_start();
		?>
		<div <?php echo $wrapper_attrs; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- sanitized by get_block_wrapper_attributes(). ?>>
			<div class="ambh-days-remaining-block__value"><?php echo esc_html( $value ); ?></div>
			<div class="ambh-days-remaining-block__label"><?php echo esc_html( $label ); ?></div>
			<div class="ambh-days-remaining-block__caption"><?php echo esc_html( $caption ); ?></div>
		</div>
		<?php
		return (string) ob_get_clean();
	}
}