<?php
/**
 * Booking Trigger Block
 *
 * @package NemtlyBooking\Blocks
 */

namespace NemtlyBooking\Blocks;

/**
 * Booking Trigger Block class
 */
class Booking_Trigger extends Abstract_Block {

    /**
     * Block Name
     *
     * @var string
     */
    protected $block_name = 'booking-trigger';

    /**
     * Register the block
     */
    public function register() {
        // Register the frontend script
        $script_asset_path = NEMTLY_BOOKING_PATH . 'assets/js/booking-trigger.asset.php';
        $script_asset_file = file_exists( $script_asset_path ) ? require $script_asset_path : [
			'dependencies' => [],
			'version'      => '1.0.0',
		];

        wp_register_script(
            'nemtly-booking-trigger',
            NEMTLY_BOOKING_URL . 'assets/js/booking-trigger.js',
            $script_asset_file['dependencies'],
            $script_asset_file['version'],
            true
        );

        $plugin_settings = get_option( 'nemtly_booking_settings', [] );

        // Localize script
        wp_localize_script(
            'nemtly-booking-trigger',
            'nemtlyBookingTrigger',
            [
                'ajaxUrl' => admin_url( 'admin-ajax.php' ),
                'nonce'   => wp_create_nonce( 'nemtly_nonce' ),
                'legal'   => [
                    'terms_url'   => esc_url( (string) apply_filters( 'nemtly_booking_terms_url', '' ) ),
                    'privacy_url' => esc_url( (string) apply_filters( 'nemtly_booking_privacy_url', (string) get_privacy_policy_url() ) ),
                ],
                'captcha' => [
                    'type'          => $plugin_settings['captcha_type'] ?? 'disabled',
                    'site_key'      => 'turnstile' === ( $plugin_settings['captcha_type'] ?? '' )
                        ? esc_attr( $plugin_settings['turnstile_site_key'] ?? '' )
                        : '',
                    'site_key_name' => 'turnstile',
                ],
            ]
        );

        // Use parent registration (metadata-based)
        parent::register();
    }

    /**
     * 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 '';
        }

        // Check if bookable
        // For now assume all events are bookable or check meta
        // $is_bookable = get_post_meta( $post_id, 'nemtly_is_bookable', true );

        /**
         * Filter the CSS classes applied to the Booking Trigger 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_booking_trigger_classes', [ 'nemtly-booking-trigger-wrapper' ], $attributes, $post_id );

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

        $button_text = isset( $attributes['text'] ) ? $attributes['text'] : __( 'Book Now', 'nemtly-booking' );

        // Enqueue the frontend script
        wp_enqueue_script( 'nemtly-booking-trigger' );
        wp_enqueue_style( 'nemtly-booking-form-styles', NEMTLY_BOOKING_URL . 'assets/css/booking-form.css', [], NEMTLY_BOOKING_VERSION );

        /**
         * Filter the resolved render data for the Booking Trigger block.
         *
         * @since PBOOKING_SINCE
         *
         * @param array $data       {
         *     @type string $button_text Button label text.
         *     @type string $button_class CSS classes applied to the button element.
         * }
         * @param array $attributes Block attributes.
         * @param int   $post_id    Event post ID.
         */
        $data = apply_filters(
            'nemtly_booking_block_booking_trigger_data',
            [
                'button_text'  => $button_text,
                'button_class' => 'nemtly-booking-trigger-btn nb-px-6 nb-py-2 nb-bg-blue-600 nb-text-white nb-rounded-lg hover:nb-bg-blue-700 nb-transition-colors',
            ],
            $attributes,
            $post_id
        );

        $data_attributes = sprintf(
            'data-event-id="%s" data-event-title="%s"',
            esc_attr( $post_id ),
            esc_attr( get_the_title( $post_id ) )
        );

        $output = sprintf(
            '<div %1$s><button class="%2$s" %3$s>%4$s</button></div>',
            $wrapper_attributes,
            esc_attr( $data['button_class'] ),
            $data_attributes,
            esc_html( $data['button_text'] )
        );

        /**
         * Filter the final rendered HTML of the Booking Trigger 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_booking_trigger_html', $output, $data, $attributes, $post_id );
    }
}
