import { createAction, PieceAuth, Property } from '@activepieces/pieces-framework';
import { httpClient, HttpMethod } from '@activepieces/pieces-common';
import { MEMPOOL_API_BASE_URL } from '../../common';

export const getBlocksBulk = createAction({
 auth:PieceAuth.None(),
    name: 'get_blocks_bulk',
    displayName: 'Get Blocks (Bulk)',
    description: 'Returns details on the range of blocks between minHeight and maxHeight, inclusive, up to 10 blocks',
    audience: 'both',
    aiMetadata: { description: 'Retrieve details for a contiguous range of blocks by height (minimum required, maximum optional and defaulting to the current tip), capped at 10 blocks per call. Pick this for surveying several consecutive blocks at once rather than fetching them one by one. Read-only.', idempotent: true },
    // category: 'Blocks',
    props: {
        minHeight: Property.Number({
            displayName: 'Minimum Height',
            description: 'The starting block height',
            required: true
        }),
        maxHeight: Property.Number({
            displayName: 'Maximum Height',
            description: 'Optional: The ending block height. If not specified, defaults to the current tip',
            required: false
        })
    },
    async run({ propsValue }) {
        const url = propsValue.maxHeight !== undefined ?
            `${MEMPOOL_API_BASE_URL}/api/v1/blocks-bulk/${propsValue.minHeight}/${propsValue.maxHeight}` :
            `${MEMPOOL_API_BASE_URL}/api/v1/blocks-bulk/${propsValue.minHeight}`;

        const response = await httpClient.sendRequest({
            method: HttpMethod.GET,
            url: url,
        });
        return response.body;
    },
});