/**
 * Editor UI for the `aieo/google-reviews` block.
 *
 * Surfaces the block's existing server-side attributes in the sidebar
 * so operators can pick mode / layout / minRating / etc. from
 * dropdowns + range sliders instead of "Edit as HTML". The block's
 * actual render still happens server-side via `render_callback` in
 * `class-aieo-google-reviews-block.php` — this file only adds the
 * editor's controls + a live ServerSideRender preview.
 *
 * No build step: classic WP-script using `wp.element.createElement`
 * and `wp.*` globals so we can ship a single static .js file.
 */
( function ( wp ) {
    'use strict';

    if ( ! wp || ! wp.blocks || ! wp.element ) { return; }

    var el                = wp.element.createElement;
    var Fragment          = wp.element.Fragment;
    var registerBlockType = wp.blocks.registerBlockType;
    var InspectorControls = wp.blockEditor && wp.blockEditor.InspectorControls;
    var PanelBody         = wp.components && wp.components.PanelBody;
    var SelectControl     = wp.components && wp.components.SelectControl;
    var RangeControl      = wp.components && wp.components.RangeControl;
    var ToggleControl     = wp.components && wp.components.ToggleControl;
    var TextControl       = wp.components && wp.components.TextControl;
    var ServerSideRender  = ( wp.serverSideRender && wp.serverSideRender.default )
        || wp.serverSideRender
        || ( wp.editor && wp.editor.ServerSideRender );
    var __                = ( wp.i18n && wp.i18n.__ ) || function ( s ) { return s; };

    if ( ! InspectorControls || ! PanelBody || ! SelectControl || ! ServerSideRender ) {
        return;
    }

    registerBlockType( 'aieo/google-reviews', {
        title: __( 'AIEO Google Reviews', 'ai-eshop-optimizer' ),
        description: __( 'Live Google reviews pulled from the local cache.', 'ai-eshop-optimizer' ),
        category: 'widgets',
        icon: 'star-filled',
        // Attributes are declared server-side; we still mirror them here
        // so the editor knows the defaults when the block is freshly
        // inserted (without `attributes`, the editor wouldn't know to
        // pre-fill the controls).
        attributes: Object.assign( {
            mode:           { type: 'string',  default: 'single' },
            locationSlug:   { type: 'string',  default: '' },
            minRating:      { type: 'number',  default: 5 },
            maxCount:       { type: 'number',  default: 5 },
            showOverall:    { type: 'boolean', default: true },
            showRateUs:     { type: 'boolean', default: true },
            showMapsButton: { type: 'boolean', default: false },
            layout:         { type: 'string',  default: 'grid' },
            presentation:   { type: 'string',  default: 'static' },
            autoplaySec:    { type: 'number',  default: 6 },
            heading:        { type: 'string',  default: '' }
        }, ( window.aieoBlockHeader && window.aieoBlockHeader.attributes() ) || {} ),
        supports: { align: [ 'wide', 'full' ], anchor: true, spacing: { margin: true, padding: true }, typography: { fontSize: true, lineHeight: true }, color: { background: true, text: true } },

        edit: function ( props ) {
            var attrs = props.attributes;
            var set   = props.setAttributes;

            var modeOptions = [
                { value: 'single',     label: __( 'Single location (auto-resolved from current post)', 'ai-eshop-optimizer' ) },
                { value: 'all_mixed',  label: __( 'Mixed feed (reviews from all locations, by date)',  'ai-eshop-optimizer' ) },
                { value: 'cards_grid', label: __( 'Cards grid (one card per location)',                'ai-eshop-optimizer' ) }
            ];

            var layoutOptions = [
                { value: 'list',     label: __( 'List (stacked, one under the other)', 'ai-eshop-optimizer' ) },
                { value: 'grid',     label: __( 'Grid', 'ai-eshop-optimizer' ) },
                { value: 'carousel', label: __( 'Carousel (horizontal scroll)', 'ai-eshop-optimizer' ) }
            ];

            var presentationOptions = [
                { value: 'static',  label: __( 'Static (all visible)',                  'ai-eshop-optimizer' ) },
                { value: 'slider',  label: __( 'Auto-slider (right→left)',              'ai-eshop-optimizer' ) },
                { value: 'rotator', label: __( 'Rotator (one card, fades up bottom→top)', 'ai-eshop-optimizer' ) }
            ];

            var inspector = el(
                InspectorControls,
                {},
                el(
                    PanelBody,
                    { title: __( 'Source', 'ai-eshop-optimizer' ), initialOpen: true },
                    el( SelectControl, {
                        label: __( 'Mode', 'ai-eshop-optimizer' ),
                        value: attrs.mode,
                        options: modeOptions,
                        onChange: function ( v ) { set( { mode: v } ); }
                    } ),
                    attrs.mode === 'single' && el( TextControl, {
                        label: __( 'Location slug (override)', 'ai-eshop-optimizer' ),
                        help: __( 'Leave empty to auto-resolve from the current post (recommended on store-detail pages).', 'ai-eshop-optimizer' ),
                        value: attrs.locationSlug || '',
                        onChange: function ( v ) { set( { locationSlug: v } ); }
                    } )
                ),
                el(
                    PanelBody,
                    { title: __( 'Filtering', 'ai-eshop-optimizer' ), initialOpen: true },
                    el( RangeControl, {
                        label: __( 'Minimum rating (★)', 'ai-eshop-optimizer' ),
                        help: __( 'Only show reviews at or above this score. Default 5★ — set to 4 to include 4-star, 1 to show all.', 'ai-eshop-optimizer' ),
                        value: attrs.minRating,
                        min: 1, max: 5, step: 1,
                        onChange: function ( v ) { set( { minRating: parseInt( v, 10 ) || 1 } ); }
                    } ),
                    el( RangeControl, {
                        label: __( 'How many reviews to show', 'ai-eshop-optimizer' ),
                        value: attrs.maxCount,
                        min: 0, max: 50, step: 1,
                        onChange: function ( v ) { set( { maxCount: parseInt( v, 10 ) || 0 } ); }
                    } )
                ),
                el(
                    PanelBody,
                    { title: __( 'Layout', 'ai-eshop-optimizer' ), initialOpen: true },
                    attrs.mode !== 'cards_grid' && el( SelectControl, {
                        label: __( 'Layout', 'ai-eshop-optimizer' ),
                        value: attrs.layout,
                        options: layoutOptions,
                        onChange: function ( v ) { set( { layout: v } ); }
                    } ),
                    attrs.mode !== 'cards_grid' && el( SelectControl, {
                        label: __( 'Presentation', 'ai-eshop-optimizer' ),
                        value: attrs.presentation,
                        options: presentationOptions,
                        onChange: function ( v ) { set( { presentation: v } ); }
                    } ),
                    ( attrs.presentation === 'slider' || attrs.presentation === 'rotator' ) && el( RangeControl, {
                        label: __( 'Autoplay seconds', 'ai-eshop-optimizer' ),
                        value: attrs.autoplaySec,
                        min: 2, max: 60, step: 1,
                        onChange: function ( v ) { set( { autoplaySec: parseInt( v, 10 ) || 6 } ); }
                    } )
                ),
                el(
                    PanelBody,
                    { title: __( 'Decorations', 'ai-eshop-optimizer' ), initialOpen: false },
                    el( TextControl, {
                        label: __( 'Heading (optional)', 'ai-eshop-optimizer' ),
                        value: attrs.heading || '',
                        onChange: function ( v ) { set( { heading: v } ); }
                    } ),
                    attrs.mode !== 'cards_grid' && el( ToggleControl, {
                        label: __( 'Show overall rating ribbon', 'ai-eshop-optimizer' ),
                        checked: !! attrs.showOverall,
                        onChange: function ( v ) { set( { showOverall: !! v } ); }
                    } ),
                    el( ToggleControl, {
                        label: __( 'Show "Rate us" button', 'ai-eshop-optimizer' ),
                        checked: !! attrs.showRateUs,
                        onChange: function ( v ) { set( { showRateUs: !! v } ); }
                    } ),
                    attrs.mode === 'single' && el( ToggleControl, {
                        label: __( 'Show "Open in Maps" button', 'ai-eshop-optimizer' ),
                        checked: !! attrs.showMapsButton,
                        onChange: function ( v ) { set( { showMapsButton: !! v } ); }
                    } )
                ),
                ( window.aieoBlockHeader && window.aieoBlockHeader.panel ) ? window.aieoBlockHeader.panel( attrs, set ) : null
            );

            var preview = el( ServerSideRender, {
                block: 'aieo/google-reviews',
                attributes: attrs
            } );

            return el( Fragment, {}, inspector, preview );
        },

        // Server-rendered — no save() output.
        save: function () { return null; }
    } );

} )( window.wp );
