<?php
if ( ! defined( 'ABSPATH' ) ) exit;

/**
 * Gutenberg block registration and server-side rendering.
 *
 * Uses the existing TMNL_Shortcode::render() so the block output
 * is always identical to the shortcode output.
 */
class TMNL_Block {

    /** @var TMNL_Shortcode */
    private $shortcode;

    public function __construct( $shortcode ) {
        $this->shortcode = $shortcode;
        add_action( 'init', array( $this, 'register_block' ) );
    }

    public function register_block() {
        // Register editor script manually (no webpack / no .asset.php needed).
        wp_register_script(
            'kuchikomi-wall-block-editor',
            TMNL_PLUGIN_URL . 'blocks/kuchikomi-wall/index.js',
            array( 'wp-blocks', 'wp-element', 'wp-block-editor', 'wp-components', 'wp-i18n' ),
            TMNL_VERSION,
            true
        );

        // Wire JS translations so __() in index.js picks up .po strings.
        wp_set_script_translations(
            'kuchikomi-wall-block-editor',
            'elplan-kuchikomi-wall',
            TMNL_PLUGIN_DIR . 'languages'
        );

        // Register block type.  block.json supplies name, attributes, supports.
        // render_callback and editor_script are added here because they are
        // runtime concerns that don't belong in the static JSON file.
        register_block_type(
            TMNL_PLUGIN_DIR . 'blocks/kuchikomi-wall',
            array(
                'editor_script'   => 'kuchikomi-wall-block-editor',
                'render_callback' => array( $this, 'render' ),
            )
        );
    }

    /**
     * Render callback — called by WordPress on each page load where the block
     * is present.  Maps camelCase block attributes to the shortcode's
     * snake_case parameter names, then delegates to TMNL_Shortcode::render().
     *
     * @param array $attributes Block attributes from the editor.
     * @return string HTML output.
     */
    public function render( $attributes ) {
        $atts = array(
            'limit'      => isset( $attributes['limit'] )     ? intval( $attributes['limit'] )                   : 6,
            'source'     => isset( $attributes['source'] )    ? sanitize_text_field( $attributes['source'] )     : 'all',
            'min_rating' => isset( $attributes['minRating'] ) ? intval( $attributes['minRating'] )               : 1,
            'columns'    => isset( $attributes['columns'] )   ? intval( $attributes['columns'] )                 : 3,
            'template'   => isset( $attributes['template'] )  ? sanitize_text_field( $attributes['template'] )   : 'card',
            'color'      => isset( $attributes['color'] )     ? sanitize_text_field( $attributes['color'] )      : 'light',
        );

        $content = $this->shortcode->render( $atts );

        if ( empty( $content ) ) {
            return '';
        }

        // Wrap with block wrapper attributes so align (wide/full) works.
        return '<div ' . get_block_wrapper_attributes() . '>' . $content . '</div>';
    }
}
