/**
 * @file assets/js/blocks/block-author-directory.js
 * Registers the mbs/author-directory 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/author-directory', {
        apiVersion: 3,
        title: 'Author Directory (PRO)',
        description: 'Full author grid with bio, book counts, and A-Z filter strip. Requires PRO license.',
        icon: 'groups',
        category: 'mbs',
        keywords: [ 'authors', 'directory', 'list', 'writers', 'browse' ],

        attributes: {
            columns:             { type: 'number',  default: 3            },
            show_count:          { type: 'boolean', default: true         },
            show_bio:            { type: 'boolean', default: true         },
            bio_length:          { type: 'number',  default: 120          },
            show_website:        { type: 'boolean', default: true         },
            show_featured_first: { type: 'boolean', default: true         },
            show_filter:         { type: 'boolean', default: true         },
            orderby:             { type: 'string',  default: 'name'       },
            order:               { type: 'string',  default: 'ASC'        },
            sort:                { type: 'string',  default: 'first_name' },
        },

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

            return el(
                Fragment,
                {},
                el(
                    InspectorControls,
                    {},
                    el(
                        PanelBody,
                        { title: 'Author Directory Settings', initialOpen: true },
                        el(RangeControl, {
                            label: 'Columns',
                            value: attributes.columns,
                            onChange: function (value) { setAttributes({ columns: value }); },
                            min: 1,
                            max: 6,
                        }),
                        el(RangeControl, {
                            label: 'Bio excerpt length (chars)',
                            value: attributes.bio_length,
                            onChange: function (value) { setAttributes({ bio_length: value }); },
                            min: 20,
                            max: 500,
                        }),
                        el(ToggleControl, {
                            label: 'Show book count',
                            checked: attributes.show_count,
                            onChange: function (value) { setAttributes({ show_count: value }); },
                        }),
                        el(ToggleControl, {
                            label: 'Show bio excerpt',
                            checked: attributes.show_bio,
                            onChange: function (value) { setAttributes({ show_bio: value }); },
                        }),
                        el(ToggleControl, {
                            label: 'Show website link',
                            checked: attributes.show_website,
                            onChange: function (value) { setAttributes({ show_website: value }); },
                        }),
                        el(ToggleControl, {
                            label: 'Featured authors first',
                            checked: attributes.show_featured_first,
                            onChange: function (value) { setAttributes({ show_featured_first: value }); },
                        }),
                        el(ToggleControl, {
                            label: 'Show A-Z filter strip',
                            checked: attributes.show_filter,
                            onChange: function (value) { setAttributes({ show_filter: value }); },
                        }),
                        el(SelectControl, {
                            label: 'Sort authors by',
                            value: attributes.sort,
                            options: [
                                { label: 'First name', value: 'first_name' },
                                { label: 'Last name',  value: 'last_name'  },
                                { label: 'Sort name (article-stripped)', value: 'name_sort' },
                            ],
                            onChange: function (value) { setAttributes({ sort: value }); },
                        }),
                        el(SelectControl, {
                            label: 'Sort direction',
                            value: attributes.order,
                            options: [
                                { label: 'A to Z', value: 'ASC'  },
                                { label: 'Z to A', value: 'DESC' },
                            ],
                            onChange: function (value) { setAttributes({ order: value }); },
                        })
                    )
                ),
                el(
                    'div',
                    useBlockProps({ className: 'mbs-block mbs-block-author-directory' }),
                    el(ServerSideRender, {
                        block: 'mbs/author-directory',
                        attributes: attributes,
                    })
                )
            );
        },

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

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