<?php
/**
 * Render callback for the full-name block.
 *
 * @package PH_Peeps
 * @since 1.0.0
 */

namespace PH_Peeps\Blocks;

// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Render the Full Name block.
 *
 * @param array    $attributes The block attributes containing name display settings.
 * @param WP_Block $block      Block instance.
 * @return string             The rendered HTML for the full name block.
 */
function ph_peeps_render_full_name_block( $attributes, $block ) {

	$post_id = isset( $block->context['postId'] ) ? $block->context['postId'] : get_the_ID();

	// Bail early if no post ID.
	if ( empty( $post_id ) ) {
		if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
			error_log( 'WP Peeps: Full Name block - No post ID available' );
		}
		return '';
	}

	// Get and sanitize attributes.
	$show_first  = ! empty( $attributes['showFirst'] );
	$show_middle = ! empty( $attributes['showMiddle'] );
	$show_last   = ! empty( $attributes['showLast'] );
	$tag_name    = sanitize_key( $attributes['tagName'] ?? 'h2' );
	$make_link   = ! empty( $attributes['makeLink'] );

	// Get name parts from meta.
	$first_name  = get_post_meta( $post_id, 'ph_peeps_first_name', true );
	$middle_name = get_post_meta( $post_id, 'ph_peeps_middle_name', true );
	$last_name   = get_post_meta( $post_id, 'ph_peeps_last_name', true );

	// Build name parts array.
	$name_parts = array();
	if ( $show_first && ! empty( $first_name ) ) {
		$name_parts[] = sanitize_text_field( $first_name );
	}
	if ( $show_middle && ! empty( $middle_name ) ) {
		$name_parts[] = sanitize_text_field( $middle_name );
	}
	if ( $show_last && ! empty( $last_name ) ) {
		$name_parts[] = sanitize_text_field( $last_name );
	}

	// Return early if no name parts.
	if ( empty( $name_parts ) ) {
		return '';
	}

	$full_name = implode( ' ', $name_parts );

	// Get block wrapper attributes.
	$wrapper_attributes = get_block_wrapper_attributes();

	// Handle link if enabled.
	if ( $make_link ) {
		$link_attrs = array(
			'href' => esc_url( get_permalink( $post_id ) ),
		);

		if ( ! empty( $attributes['openInNewTab'] ) ) {
			$link_attrs['target'] = '_blank';
			// Add rel="noopener" for security when target="_blank" is used.
			$raw_rel           = ! empty( $attributes['linkRel'] ) ? sanitize_text_field( $attributes['linkRel'] ) : '';
			$link_rel          = ! empty( $raw_rel ) ? $raw_rel . ' noopener' : 'noopener';
			$link_attrs['rel'] = trim( $link_rel );
		} elseif ( ! empty( $attributes['linkRel'] ) ) {
			$link_attrs['rel'] = sanitize_text_field( $attributes['linkRel'] );
		}

		// Build link attributes string.
		$link_attrs_str = '';
		foreach ( $link_attrs as $key => $value ) {
			$link_attrs_str .= sprintf( ' %s="%s"', esc_attr( $key ), esc_attr( $value ) );
		}

		$full_name = sprintf( '<a%s>%s</a>', $link_attrs_str, esc_html( $full_name ) );
	}

	// Build and return the final HTML.
	return sprintf(
		'<%1$s %2$s>%3$s</%1$s>',
		tag_escape( $tag_name ),
		$wrapper_attributes,
		$full_name
	);
}
