<?php
/**
 * Event Price Block
 *
 * @package NemtlyBooking\Blocks
 */

namespace NemtlyBooking\Blocks;

/**
 * Event Price Block class
 */
class Event_Price extends Abstract_Block {

    /**
     * Block Name
     *
     * @var string
     */
    protected $block_name = 'price';

    /**
     * Render the block
     *
     * @param array    $attributes Block attributes.
     * @param string   $content    Block content.
     * @param \WP_Block $block      Block instance.
     * @return string
     */
    public function render( $attributes, $content, $block ) {
        $post_id = isset( $block->context['postId'] ) ? $block->context['postId'] : 0;

        // Verify post type
        if ( empty( $post_id ) || get_post_type( $post_id ) !== 'nemtly_event' ) {
            return '';
        }

        $price    = get_post_meta( $post_id, 'nemtly_price', true );
        $currency = get_option( 'nemtly_currency_symbol', '$' );

        // If price is empty or 0, maybe show "Free"? Or valid price.
        // Assuming price is stored as numeric.
        if ( ! is_numeric( $price ) && empty( $price ) ) {
            return '';
        }

        /**
         * Filter the CSS classes applied to the Event Price block wrapper.
         *
         * @since PBOOKING_SINCE
         *
         * @param string[] $classes    Wrapper CSS classes.
         * @param array    $attributes Block attributes.
         * @param int      $post_id    Event post ID.
         */
        $classes = apply_filters( 'nemtly_booking_block_event_price_classes', [ 'nemtly-event-price' ], $attributes, $post_id );

        $wrapper_attributes = get_block_wrapper_attributes(
            [
                'class' => implode( ' ', $classes ),
            ]
        );

        $display_price = $currency . $price;
        if ( 0.0 === (float) $price ) {
            $display_price = __( 'Free', 'nemtly-booking' );
        }

        /**
         * Filter the resolved render data for the Event Price block.
         *
         * @since PBOOKING_SINCE
         *
         * @param array $data       {
         *     @type string $display_price Formatted price text (e.g. "$10" or "Free").
         * }
         * @param array $attributes Block attributes.
         * @param int   $post_id    Event post ID.
         */
        $data = apply_filters(
            'nemtly_booking_block_event_price_data',
            [
                'display_price' => $display_price,
            ],
            $attributes,
            $post_id
        );

        $output = sprintf(
            '<div %1$s><span class="nemtly-price-amount">%2$s</span></div>',
            $wrapper_attributes,
            esc_html( $data['display_price'] )
        );

        /**
         * Filter the final rendered HTML of the Event Price block.
         *
         * @since PBOOKING_SINCE
         *
         * @param string $output     Rendered block HTML.
         * @param array  $data       Render data used to build the output.
         * @param array  $attributes Block attributes.
         * @param int    $post_id    Event post ID.
         */
        return apply_filters( 'nemtly_booking_block_event_price_html', $output, $data, $attributes, $post_id );
    }
}
