/**
 * @file assets/js/blocks/block-bestseller.js
 * Registers the mbs/bestseller Gutenberg block.
 * Depends on mbs-blocks-editor (wp.* globals + window.mbsBlocksCore helpers).
 * Helpers used: none
 * @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;

    // ============================================================================
    registerBlockType('mbs/bestseller', {
        apiVersion: 3,
        title: 'Bestseller List (PRO)',
        description: 'Display books ranked by Amazon sales data. Requires PRO license and PA-API credentials.',
        icon: 'star-filled',
        category: 'mbs',
        keywords: [ 'bestseller', 'popular', 'top books', 'ranking', 'amazon' ],

        attributes: {
            limit:  { type: 'number', default: 10     },
            layout: { type: 'string', default: 'grid' },
        },

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

            return el(
                Fragment,
                {},
                el(
                    InspectorControls,
                    {},
                    el(
                        PanelBody,
                        { title: 'Bestseller Settings', initialOpen: true },
                        el(RangeControl, {
                            label: 'Number of Books',
                            value: attributes.limit,
                            onChange: function (value) {
                                setAttributes({ limit: value });
                            },
                            min: 1,
                            max: 20,
                        }),
                        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-bestseller' }),
                    el(ServerSideRender, {
                        block: 'mbs/bestseller',
                        attributes: attributes,
                    })
                )
            );
        },

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

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