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

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

    // ============================================================================
    registerBlockType('mbs/genre', {
        apiVersion: 3,
        title: 'Genre',
        description: 'Display all books in a genre.',
        icon: 'tag',
        category: 'mbs',
        keywords: [ 'genre', 'category', 'books', 'fiction', 'filter' ],

        attributes: {
            slug:     { type: 'string', default: '' },
            per_page: { type: 'number', default: 20 },
        },

        edit: function (props) {
            const { attributes, setAttributes } = props;
            const termOptions = getTermOptions('mbs_genre');

            return el(
                Fragment,
                {},
                el(
                    InspectorControls,
                    {},
                    el(
                        PanelBody,
                        { title: 'Genre Settings', initialOpen: true },
                        el(SelectControl, {
                            label: 'Select Genre',
                            value: attributes.slug,
                            options: termOptions,
                            onChange: function (value) {
                                setAttributes({ slug: value });
                            },
                        }),
                        el(RangeControl, {
                            label: 'Books per page (-1 = all)',
                            value: attributes.per_page,
                            onChange: function (value) {
                                setAttributes({ per_page: value });
                            },
                            min: -1,
                            max: 100,
                        })
                    )
                ),
                el(
                    'div',
                    useBlockProps({ className: 'mbs-block mbs-block-genre' }),
                    attributes.slug
                        ? el(ServerSideRender, {
                            block: 'mbs/genre',
                            attributes: attributes,
                          })
                        : el(
                            'div',
                            { className: 'mbs-block-placeholder' },
                            'Select a genre from the block settings. Manage genres in My Book Showroom → Settings → Genres.'
                          )
                )
            );
        },

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

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