<?php
/**
 * Plugin Name: BW Comparison Table
 * Description: A custom block for The Venetian comparison tables.
 * Version: 1.1.0
 * Author: Bowden Works
 * Author URI: https://bowdenworks.com
 * License: GPL-2.0-or-later
 * License URI: https://www.gnu.org/licenses/gpl-2.0.html
 * Text Domain: bw-comparison
 */

if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

/**
 * Register the comparison table blocks
 */
function bw_register_comparison_blocks() {
    // Register the main comparison table block
    register_block_type( __DIR__ . '/build', array(
        'render_callback' => 'bw_render_comparison_table',
    ) );

    // Register the comparison row block (child)
    register_block_type( 'bw/comparison-row', array(
        'render_callback' => 'bw_render_comparison_row',
        'attributes' => array(
            'label' => array(
                'type' => 'string',
                'default' => '',
            ),
            'venetianTitle' => array(
                'type' => 'string',
                'default' => '',
            ),
            'venetianDesc' => array(
                'type' => 'string',
                'default' => '',
            ),
            'compTitle' => array(
                'type' => 'string',
                'default' => '',
            ),
            'compDesc' => array(
                'type' => 'string',
                'default' => '',
            ),
        ),
    ) );
}
add_action( 'init', 'bw_register_comparison_blocks' );

/**
 * Render the comparison table block
 */
function bw_render_comparison_table( $attributes, $content ) {
    $col1_header = isset( $attributes['col1Header'] ) ? $attributes['col1Header'] : 'Perspective';
    $col2_header = isset( $attributes['col2Header'] ) ? $attributes['col2Header'] : 'The Venetian Experience';
    $col3_header = isset( $attributes['col3Header'] ) ? $attributes['col3Header'] : 'Central Resorts';

    $wrapper_attributes = get_block_wrapper_attributes( array( 'class' => 'venetian-compare-v2' ) );

    ob_start();
    ?>
    <div <?php echo $wrapper_attributes; ?>>
        <div class="v2-row header-row">
            <div class="v2-col-label"><span><?php echo wp_kses_post( $col1_header ); ?></span></div>
            <div class="v2-col-venetian"><span><?php echo wp_kses_post( $col2_header ); ?></span></div>
            <div class="v2-col-competitor"><span><?php echo wp_kses_post( $col3_header ); ?></span></div>
        </div>
        <?php echo $content; ?>
    </div>
    <?php
    return ob_get_clean();
}

/**
 * Render a comparison row block
 */
function bw_render_comparison_row( $attributes, $content ) {
    $label = isset( $attributes['label'] ) ? $attributes['label'] : '';
    $venetian_title = isset( $attributes['venetianTitle'] ) ? $attributes['venetianTitle'] : '';
    $venetian_desc = isset( $attributes['venetianDesc'] ) ? $attributes['venetianDesc'] : '';
    $comp_title = isset( $attributes['compTitle'] ) ? $attributes['compTitle'] : '';
    $comp_desc = isset( $attributes['compDesc'] ) ? $attributes['compDesc'] : '';

    ob_start();
    ?>
    <div class="v2-row">
        <div class="v2-col-label"><span><?php echo wp_kses_post( $label ); ?></span></div>
        <div class="v2-col-venetian">
            <div class="v2-title">
                <span class="v2-check-badge">&#10003;</span>
                <span class="venetian-text"><?php echo wp_kses_post( $venetian_title ); ?></span>
            </div>
            <?php if ( ! empty( $venetian_desc ) ) : ?>
                <p class="v2-desc v2-desc-venetian"><?php echo wp_kses_post( $venetian_desc ); ?></p>
            <?php endif; ?>
        </div>
        <div class="v2-col-competitor">
            <div class="v2-title-comp">
                <span class="comp-text"><?php echo wp_kses_post( $comp_title ); ?></span>
            </div>
            <?php if ( ! empty( $comp_desc ) ) : ?>
                <p class="v2-desc v2-desc-comp"><?php echo wp_kses_post( $comp_desc ); ?></p>
            <?php endif; ?>
        </div>
    </div>
    <?php
    return ob_get_clean();
}
