<?php
/**
 * BW Step Layout Block - Server-side registration
 */

// Register the block
function bw_register_step_layout_block() {
    // Register block styles
    wp_register_style(
        'bw-step-layout-style',
        get_stylesheet_directory_uri() . '/blocks/bw-step-layout/style-index.css',
        array(),
        filemtime(get_stylesheet_directory() . '/blocks/bw-step-layout/style-index.css')
    );

    // Register block editor styles
    wp_register_style(
        'bw-step-layout-editor',
        get_stylesheet_directory_uri() . '/blocks/bw-step-layout/index.css',
        array(),
        filemtime(get_stylesheet_directory() . '/blocks/bw-step-layout/index.css')
    );

    // Register the block
    register_block_type('bw/step-layout', array(
        'api_version' => 2,
        'title' => __('Step Layout', 'kadence-child'),
        'description' => __('A responsive step layout block with three cards', 'kadence-child'),
        'category' => 'design',
        'icon' => 'layout',
        'keywords' => array('step', 'cards', 'layout'),
        'supports' => array(
            'align' => array('wide', 'full'),
            'html' => false,
            'customClassName' => true,
        ),
        'attributes' => array(
            'stepDirection' => array(
                'type' => 'string',
                'default' => 'low-to-high',
            ),
            'cards' => array(
                'type' => 'array',
                'default' => array(
                    array(
                        'title' => 'Card 1',
                        'description' => 'Description for card 1',
                        'buttonText' => 'EXPLORE',
                        'buttonUrl' => '#',
                        'backgroundImage' => '',
                    ),
                    array(
                        'title' => 'Card 2',
                        'description' => 'Description for card 2',
                        'buttonText' => 'EXPLORE',
                        'buttonUrl' => '#',
                        'backgroundImage' => '',
                    ),
                    array(
                        'title' => 'Card 3',
                        'description' => 'Description for card 3',
                        'buttonText' => 'EXPLORE',
                        'buttonUrl' => '#',
                        'backgroundImage' => '',
                    ),
                ),
            ),
        ),
        'render_callback' => 'bw_render_step_layout_block',
        'style' => 'bw-step-layout-style',
        'editor_style' => 'bw-step-layout-editor',
    ));
}
add_action('init', 'bw_register_step_layout_block');

// Render callback
function bw_render_step_layout_block($attributes, $content) {
    $step_direction = isset($attributes['stepDirection']) ? $attributes['stepDirection'] : 'low-to-high';
    $cards = isset($attributes['cards']) ? $attributes['cards'] : array();
    
    // Get alignment class
    $align_class = '';
    if (isset($attributes['align'])) {
        $align_class = 'align' . $attributes['align'];
    }
    
    $wrapper_class = 'wp-block-bw-step-layout bw-step-layout bw-step-layout--' . esc_attr($step_direction);
    if ($align_class) {
        $wrapper_class .= ' ' . $align_class;
    }
    
    ob_start();
    ?>
    <div class="<?php echo esc_attr($wrapper_class); ?>">
        <div class="bw-step-layout__inner">
            <?php 
            foreach ($cards as $index => $card) : 
                $card_class = 'bw-step-layout__card bw-step-layout__card--' . ($index + 1);
                $has_image = !empty($card['backgroundImage']);
            ?>
                <div class="<?php echo esc_attr($card_class); ?>" <?php if ($has_image) : ?>data-has-image="true"<?php endif; ?>>
                    <?php if ($has_image) : ?>
                        <div class="bw-step-layout__background">
                            <img src="<?php echo esc_url($card['backgroundImage']); ?>" alt="" />
                        </div>
                    <?php endif; ?>
                    
                    <div class="bw-step-layout__content">
                        <h3 class="bw-step-layout__title"><?php echo esc_html($card['title']); ?></h3>
                        <p class="bw-step-layout__description"><?php echo esc_html($card['description']); ?></p>
                        
                        <?php if (!empty($card['buttonText']) && !empty($card['buttonUrl'])) : ?>
                            <a href="<?php echo esc_url($card['buttonUrl']); ?>" class="bw-step-layout__button">
                                <?php echo esc_html($card['buttonText']); ?>
                            </a>
                        <?php endif; ?>
                    </div>
                </div>
            <?php endforeach; ?>
        </div>
    </div>
    <?php
    return ob_get_clean();
}