<?php
function render_metafield_block( $attributes, $content, $block ) {
    ob_start();

    // Determine block name and class
    $block_name       = str_replace( "stepfox/", "", strtolower( $block->name ) );
    $block_name_class = str_replace( "-", "_", $block_name );

    // Determine the post ID. Default to current post.
    $post_id = get_the_ID();
    if ( defined( 'REST_REQUEST' ) && REST_REQUEST && $block_name_class === 'metafield_block' && ! empty( $attributes['select_a_post'] ) ) {
        $post_id = $attributes['select_a_post'];
    }
    wp_reset_postdata();

    // Ensure we have a valid post ID
    if ( empty( $post_id ) ) {
        $post_id = get_the_ID();
    }

    // Default meta_field to 'post_title' if not provided.
    $meta_field = ! empty( $attributes['meta_field'] ) ? $attributes['meta_field'] : 'post_title';
    $counter    = ''; // default value

    // Determine the output string based on the meta_field.
    switch ( $meta_field ) {
        case 'counter':
            if ( defined( 'REST_REQUEST' ) && REST_REQUEST ) {
                $string = '1';
            } else {
                $counter = 'counter';
                $string  = '';
            }
            break;
        case 'post_title':
            $string = get_the_title( $post_id );
            break;
        case 'post_content':
            $post  = get_post( $post_id );
            $string = apply_filters( 'the_content', $post->post_content );
            break;
        case 'post_excerpt':
            $string = get_the_excerpt( $post_id );
            break;
        case 'featured_image':
            $string = get_the_post_thumbnail_url( $post_id );
            break;
        case 'permalink':
            $string = get_permalink( $post_id );
            break;
        case 'month':
            $string = date_i18n( 'F' );
            break;
        default:
            $string = get_post_meta( $post_id, $meta_field, true );
            break;
    }

    // Build element properties
    $className      = isset( $block->parsed_block['attrs']['className'] ) ? $block->parsed_block['attrs']['className'] : '';
    $element_props  = 'id="block_' . esc_attr( $attributes['id'] ) . '" class="' . esc_attr( $counter . ' ' . $className . ' ' . $block_name_class . ' ' . $attributes['align'] ) . '"';

    // Render output based on element type
    switch ( $attributes['element_type'] ) {
        case 'image':
            $img_src = wp_get_attachment_image_src( $string, 'full' );
            if ( $img_src ) {
                $img_src = $img_src[0];
            }
            echo '<img ' . $element_props . ' src="' . esc_url( $img_src ) . '"/>';
            break;
        case 'stat':
            echo '<img ' . $element_props . ' src="' . esc_url( $string ) . '"/>';
            break;
        case 'link':
        case 'button':
            // Use inner content if available.
            if ( ! empty( $content ) ) {
                $attributes['innerContent'] = $content;
            }
            // Open link in a new window with nofollow if required.
            $new_window = ( $meta_field === 'review_list_setting_list_link_url' ) ? 'target="_blank" rel="nofollow"' : '';
            // Remove id for link/button elements.
            $element_props = 'class="' . esc_attr( $counter . ' ' . $className . ' ' . $block_name_class . ' ' . $attributes['align'] ) . '"';
            echo '<a ' . $element_props . ' ' . $new_window . ' href="' . esc_url( $string ) . '">';
            echo do_blocks( $attributes['innerContent'] );
            echo '</a>';
            break;
        default:
            echo '<' . esc_attr( $attributes['element_type'] ) . ' ' . $element_props . '>';
            echo $string;
            echo '</' . esc_attr( $attributes['element_type'] ) . '>';
            break;
    }

    return ob_get_clean();
}
