<?php
/**
 * CalcsLive Gutenberg Block
 *
 * Registers the CalcsLive Article block for the block editor.
 */

// Prevent direct access
if (!defined('ABSPATH')) {
    exit;
}

class CalcsLive_Block {

    /**
     * Register the block
     */
    public static function register() {
        // Register block scripts and styles
        add_action('enqueue_block_editor_assets', array(__CLASS__, 'enqueue_editor_assets'));

        // Register the block
        register_block_type('calcslive/article-embed', array(
            'editor_script'   => 'calcslive-block-editor',
            'editor_style'    => 'calcslive-block-editor-style',
            'render_callback' => array(__CLASS__, 'render_block'),
            'attributes'      => array(
                'articleId' => array(
                    'type'    => 'string',
                    'default' => '',
                ),
                'width' => array(
                    'type'    => 'string',
                    'default' => '100%',
                ),
                'height' => array(
                    'type'    => 'number',
                    'default' => 600,
                ),
            ),
        ));
    }

    /**
     * Enqueue editor assets
     */
    public static function enqueue_editor_assets() {
        // Register and enqueue the block editor script
        wp_register_script(
            'calcslive-block-editor',
            CALCSLIVE_PLUGIN_URL . 'blocks/calcslive-embed/edit.js',
            array('wp-blocks', 'wp-element', 'wp-editor', 'wp-components', 'wp-i18n'),
            CALCSLIVE_VERSION,
            true
        );

        // Pass settings to JavaScript for block editor preview
        // Note: API key is passed to enable live preview. This is acceptable because:
        // 1. The key is already visible in frontend iframe URLs (public by design)
        // 2. The key is domain-bound - only works for requests from this domain
        // 3. Users with block editor access can already see rendered embeds
        wp_localize_script('calcslive-block-editor', 'calcsliveBlockSettings', array(
            'defaultWidth'  => get_option('calcslive_default_width', '100%'),
            'defaultHeight' => get_option('calcslive_default_height', '600'),
            'apiKey'        => get_option('calcslive_api_key', ''),
            'domain'        => get_option('calcslive_domain', ''),
            'embedBaseUrl'  => CALCSLIVE_EMBED_BASE_URL,
            'hasApiKey'     => !empty(get_option('calcslive_api_key', '')),
        ));

        wp_enqueue_script('calcslive-block-editor');

        // Register and enqueue editor styles
        wp_register_style(
            'calcslive-block-editor-style',
            CALCSLIVE_PLUGIN_URL . 'assets/admin.css',
            array(),
            CALCSLIVE_VERSION
        );

        wp_enqueue_style('calcslive-block-editor-style');
    }

    /**
     * Render the block on the frontend
     *
     * @param array $attributes Block attributes
     * @return string HTML output
     */
    public static function render_block($attributes) {
        // Use the shortcode handler for consistent rendering
        $shortcode_atts = array(
            'id'     => isset($attributes['articleId']) ? $attributes['articleId'] : '',
            'width'  => isset($attributes['width']) ? $attributes['width'] : '100%',
            'height' => isset($attributes['height']) ? $attributes['height'] : 600,
        );

        return CalcsLive_Shortcode::render($shortcode_atts);
    }
}
