/**
 * @file assets/js/blocks/block-related-books.js
 * Registers the mbs/related-books Gutenberg block.
 * Depends on mbs-blocks-editor (wp.* globals + window.mbsBlocksCore helpers).
 * Helpers used: getBookOptions
 * @package MyBookShowroom
 * @since   1.0.0
 */

( function ( wp, mbsCore ) {
    'use strict';

    const { registerBlockType } = wp.blocks;
    const { InspectorControls, useBlockProps } = wp.blockEditor;
    const { ServerSideRender } = wp.serverSideRender;
    const { PanelBody, SelectControl, RangeControl } = wp.components;
    const { createElement: el, Fragment } = wp.element;

    // Shared helpers provided by blocks-editor.js via window.mbsBlocksCore.
    const getBookOptions = mbsCore.getBookOptions || function() { return []; };

    // ============================================================================
    registerBlockType('mbs/related-books', {
        apiVersion: 3,
        title: 'Related Books (PRO)',
        description: 'Display books related by shared genre, series, or collection terms. Requires PRO license.',
        icon: 'networking',
        category: 'mbs',
        keywords: [ 'related', 'similar', 'books', 'recommendations', 'you might also like' ],

        attributes: {
            bookId: { type: 'number', default: 0 },
            limit:  { type: 'number', default: 6  },
            layout: { type: 'string', default: 'grid' },
        },

        edit: function (props) {
            const { attributes, setAttributes } = props;
            const bookOptions = getBookOptions();

            return el(
                Fragment,
                {},
                el(
                    InspectorControls,
                    {},
                    el(
                        PanelBody,
                        { title: 'Related Books Settings', initialOpen: true },
                        el(SelectControl, {
                            label: 'Source Book (optional)',
                            help: 'Leave at 0 to use the current page context.',
                            value: attributes.bookId,
                            options: bookOptions,
                            onChange: function (value) {
                                setAttributes({ bookId: parseInt(value, 10) || 0 });
                            },
                        }),
                        el(RangeControl, {
                            label: 'Maximum Books',
                            value: attributes.limit,
                            onChange: function (value) {
                                setAttributes({ limit: value });
                            },
                            min: 1,
                            max: 12,
                        }),
                        el(SelectControl, {
                            label: 'Layout',
                            value: attributes.layout,
                            options: [
                                { label: 'Grid', value: 'grid' },
                                { label: 'List', value: 'list' },
                            ],
                            onChange: function (value) {
                                setAttributes({ layout: value });
                            },
                        })
                    )
                ),
                el(
                    'div',
                    useBlockProps({ className: 'mbs-block mbs-block-related-books' }),
                    el(ServerSideRender, {
                        block: 'mbs/related-books',
                        attributes: attributes,
                    })
                )
            );
        },

        save: function () {
            return null; // Dynamic block - rendered by PHP via render_callback
        },
    });

} )( window.wp, window.mbsBlocksCore || {} );
