<?php
/**
 * Team Info Block
 *
 * Displays the parent team name and image for fundraisers that belong to a team.
 * Returns empty output for team pages and standalone fundraisers.
 *
 * @package AmbassadorHub\Fundraisers\Blocks\TeamInfo
 */

namespace AmbassadorHub\Fundraisers\Blocks\TeamInfo;

use AmbassadorHub\Fundraisers\FundraisersHelpers;
use AmbassadorHub\Fundraisers\FundraisersModule;
use AmbassadorHub\Fundraisers\Admin\Settings\SettingsManager;

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/TeamInfo/';
		$block_url = AMBASSADOR_HUB_URL . 'src/Fundraisers/Blocks/TeamInfo/';

		// Editor script.
		$editor_handle = 'ambh-team-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
			);
		}

		// Styles (shared between editor and frontend).
		$style_file = $block_dir . 'style.css';
		wp_register_style(
			'ambh-team-info',
			$block_url . 'style.css',
			array(),
			file_exists( $style_file ) ? (string) filemtime( $style_file ) : AMBASSADOR_HUB_VERSION
		);

		$block_args = array(
			'style'           => 'ambh-team-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
		// Editor placeholder.
		if ( defined( 'REST_REQUEST' ) && REST_REQUEST ) {
			return '<div style="padding: 20px; background: #f0f0f1; border: 1px solid #ddd; text-align: center;" class="ambh-block-preview">' . esc_html__( 'Team Info (renders on the frontend for individual fundraisers)', 'ambassadorhub' ) . '</div>';
		}

		global $post;

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

		$team_id = (int) wp_get_post_parent_id( $post );
		if ( $team_id <= 0 ) {
			return '';
		}

		// Confirm the parent is a fundraiser marked as a team.
		if ( FundraisersModule::POST_TYPE !== get_post_type( $team_id ) ) {
			return '';
		}

		if ( '1' !== get_post_meta( $team_id, FundraisersModule::META_IS_TEAM, true ) ) {
			return '';
		}

		$team = get_post( $team_id );
		if ( ! $team ) {
			return '';
		}

		// Determine if the current user can join this team (create an individual fundraiser under it).
		$can_join = FundraisersHelpers::individual_creation_allowed( $team_id );
		$join_url = '';
		if ( $can_join ) {
			$root_id  = FundraisersHelpers::get_root_fundraiser_id( $team_id );
			$join_url = add_query_arg(
				array_filter(
					array(
						'parent_id' => $team_id,
						'root_id'   => $root_id ? $root_id : null,
						'type'      => 'individual',
					)
				),
				SettingsManager::get_create_fundraiser_page_url()
			);
		}

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