/**
 * @file assets/js/blocks/block-new-releases.js
 * Registers the mbs/new-releases 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, TextControl, RangeControl, ToggleControl } = wp.components;
    const { createElement: el, Fragment } = wp.element;

    // ============================================================================
    registerBlockType('mbs/new-releases', {
        apiVersion: 3,
        title: 'New Releases',
        description: 'Display books published within a recent window. Uses mbs_publication_date to determine recency.',
        icon: 'star-filled',
        category: 'mbs',
        keywords: [ 'new releases', 'recent', 'latest', 'books', 'new' ],

        attributes: {
            limit: { type: 'number', default: 6  },
            days:  { type: 'number', default: 30 },
        },

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

            return el(
                Fragment,
                {},
                el(
                    InspectorControls,
                    {},
                    el(
                        PanelBody,
                        { title: 'New Releases Settings', initialOpen: true },
                        el(RangeControl, {
                            label: 'Number of books',
                            value: attributes.limit,
                            onChange: function (value) { setAttributes({ limit: value }); },
                            min: 1,
                            max: 24,
                        }),
                        el(SelectControl, {
                            label: 'Publication window',
                            help: 'Show books published within this many days.',
                            value: attributes.days,
                            options: [
                                { label: 'Last 30 days',  value: 30  },
                                { label: 'Last 60 days',  value: 60  },
                                { label: 'Last 90 days',  value: 90  },
                                { label: 'Last 180 days', value: 180 },
                            ],
                            onChange: function (value) { setAttributes({ days: parseInt(value, 10) }); },
                        })
                    )
                ),
                el(
                    'div',
                    useBlockProps({ className: 'mbs-block mbs-block-new-releases' }),
                    el(ServerSideRender, {
                        block: 'mbs/new-releases',
                        attributes: attributes,
                    })
                )
            );
        },

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

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