<?php
/**
 * User Info Block
 *
 * Displays the fundraiser owner's avatar, display name, and active fundraiser count.
 *
 * @package AmbassadorHub\Fundraisers\Blocks\UserInfo
 */

namespace AmbassadorHub\Fundraisers\Blocks\UserInfo;

use AmbassadorHub\Fundraisers\FundraisersModule;

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

/**
 * Block registration and server-side rendering.
 */
final class Block {
	/**
	 * Initialize the block.
	 *
	 * @return void
	 */
	public static function init(): void {
		add_action( 'init', array( self::class, 'register_block' ) );
	}

	/**
	 * Register block type, scripts, and styles.
	 *
	 * @return void
	 */
	public static function register_block(): void {
		$block_dir = AMBASSADOR_HUB_PATH . 'src/Fundraisers/Blocks/UserInfo/';
		$block_url = AMBASSADOR_HUB_URL . 'src/Fundraisers/Blocks/UserInfo/';

		$editor_handle = 'ambh-user-info-editor';
		$editor_file   = $block_dir . 'index.js';
		if ( file_exists( $editor_file ) ) {
			wp_register_script(
				$editor_handle,
				$block_url . 'index.js',
				array( 'wp-blocks', 'wp-i18n', 'wp-element', 'wp-block-editor' ),
				(string) filemtime( $editor_file ),
				true
			);
		}

		$style_file = $block_dir . 'style.css';
		wp_register_style(
			'ambh-user-info',
			$block_url . 'style.css',
			array(),
			file_exists( $style_file ) ? (string) filemtime( $style_file ) : AMBASSADOR_HUB_VERSION
		);

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

		if ( file_exists( $editor_file ) ) {
			$block_args['editor_script'] = $editor_handle;
		}

		register_block_type( $block_dir, $block_args );
	}

	/**
	 * Server-side render callback.
	 *
	 * @param array $attributes Block attributes.
	 *
	 * @return string Rendered HTML, or empty string when not applicable.
	 */
	public static function render_block( array $attributes = array() ): string { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.Found -- required by WP block render_callback signature
		if ( defined( 'REST_REQUEST' ) && REST_REQUEST ) {
			return '<div style="padding:10px 14px;background:rgba(0,0,0,0.45);border-radius:6px;display:inline-flex;align-items:center;gap:10px;color:#fff;" class="ambh-block-preview"><div style="width:36px;height:36px;border-radius:50%;background:#888;flex-shrink:0;"></div><div><div style="font-weight:600;font-size:13px;">' . esc_html__( 'Fundraiser Owner', 'ambassadorhub' ) . '</div><div style="font-size:11px;opacity:.75;">' . esc_html__( 'N active fundraisers', 'ambassadorhub' ) . '</div></div></div>';
		}

		global $post;

		if ( ! $post || FundraisersModule::POST_TYPE !== get_post_type( $post ) ) {
			return '';
		}

		$user_id = (int) $post->post_author;
		if ( $user_id <= 0 ) {
			return '';
		}

		$user = get_userdata( $user_id );
		if ( ! $user ) {
			return '';
		}

		$active_count = self::get_active_fundraiser_count( $user_id );

		ob_start();
		require __DIR__ . '/templates/user-info.php';
		return (string) ob_get_clean();
	}

	/**
	 * Count published and pending-verification fundraisers for a user.
	 *
	 * @param int $user_id WordPress user ID.
	 *
	 * @return int
	 */
	private static function get_active_fundraiser_count( int $user_id ): int {
		$query = new \WP_Query(
			array(
				'post_type'      => FundraisersModule::POST_TYPE,
				'post_status'    => array( 'publish', 'pending_verification' ),
				'author'         => $user_id,
				'posts_per_page' => -1,
				'fields'         => 'ids',
				'no_found_rows'  => false,
			)
		);

		return (int) $query->found_posts;
	}
}
