/**
 * Order Metrics Generators Definitions
 *
 * This file contains generator functions for each block defined in
 * order_metrics_blocks.js. Each generator translates a Blockly block into a JSON
 * object representing order metrics retrieval operations.
 */

export function defineOrderMetricsGenerators() {

    // Ensure the base generator is initialized and 'createOperationObject' is available
    if (!Blockly.JSON || !Blockly.JSON.createOperationObject) {
        console.error("Blockly.JSON or createOperationObject not initialized. Please ensure 'json_base_generator.js' is loaded first.");
        return;
    }

    /**
     * Helper Function: validateOrderType
     * Validates the order type and returns a default if invalid.
     *
     * @param {string} orderType - The order type to validate.
     * @returns {string} - Validated order type.
     */
    function validateOrderType(orderType) {
        const validOrderTypes = ['filled', 'unfilled'];
        if (!validOrderTypes.includes(orderType)) {
            console.error(`Invalid order type '${orderType}' in 'order_volume' block. Defaulting to 'filled'.`);
            return 'filled';
        }
        return orderType;
    }

    /**
     * Generator for 'order_volume' Block
     */
    Blockly.JSON['order_volume'] = function(block) {
        const orderType = block.getFieldValue('ORDER_TYPE');
        const validatedOrderType = validateOrderType(orderType);

        const operationObject = Blockly.JSON.createOperationObject('order_volume', {
            order_type: validatedOrderType
        });

        // Prepare output, handling any additional values
        const outputValues = [operationObject];
        const valuesArray = Blockly.JSON.processValuesInput(block);
        if (valuesArray.length > 0) {
            outputValues.push(...valuesArray);
        }

        return {
            type: 'dynamic_value',
            values: outputValues
        };
    };

    /**
     * Generator for 'filled_orders' Block
     */
    Blockly.JSON['filled_orders'] = function(block) {
        const operationObject = Blockly.JSON.createOperationObject('filled_orders', {});

        const outputValues = [operationObject];
        const valuesArray = Blockly.JSON.processValuesInput(block);
        if (valuesArray.length > 0) {
            outputValues.push(...valuesArray);
        }

        return {
            type: 'dynamic_value',
            values: outputValues
        };
    };

    /**
     * Generator for 'unfilled_orders' Block
     */
    Blockly.JSON['unfilled_orders'] = function(block) {
        const operationObject = Blockly.JSON.createOperationObject('unfilled_orders', {});

        const outputValues = [operationObject];
        const valuesArray = Blockly.JSON.processValuesInput(block);
        if (valuesArray.length > 0) {
            outputValues.push(...valuesArray);
        }

        return {
            type: 'dynamic_value',
            values: outputValues
        };
    };

    /**
     * Generator for 'order_status' Block
     */
    Blockly.JSON['order_status'] = function(block) {
        // Retrieve the 'ORDER_NAME' field input
        const orderName = (block.getFieldValue('ORDER_NAME') || 'last_order').trim();

        // Validate order name
        if (!orderName) {
            console.warn("Empty 'ORDER_NAME' in 'order_status' block. Using 'last_order'.");
        }

        // Retrieve the selected order status from the dropdown
        const orderStatus = block.getFieldValue('ORDER_STATUS'); // 'filled', 'unfilled', 'partial'
        const validStatuses = ['filled', 'unfilled', 'partial'];
        const validatedStatus = validStatuses.includes(orderStatus) ? orderStatus : 'filled';

        if (!validStatuses.includes(orderStatus)) {
            console.error(`Invalid order status '${orderStatus}' in 'order_status' block. Defaulting to 'filled'.`);
        }

        // Create operation object
        const operationObject = Blockly.JSON.createOperationObject('order_status', {
            order_name: orderName || 'last_order',
            status: validatedStatus
        });

        // Output as dynamic_value (since this produces a Boolean value)
        return operationObject;
    };
}
