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

export const getAddressCounters = createAction({
  name: 'get_address_counters',
  displayName: 'Get Address Counters',
  description: 'Get counters (transactions count, token transfers count, etc.) for an address',
  audience: 'both',
  aiMetadata: { description: 'Fetch aggregate activity counts for an Ethereum address (total transactions, token transfers, gas usage). Pick this for a quick summary of how active an address is rather than retrieving the underlying records. Read-only lookup on eth.blockscout.com; requires a 0x address hash.', idempotent: true },
  // category: 'Addresses',
  props: {
    addressHash: Property.ShortText({
      displayName: 'Address Hash',
      description: 'Hash of the address to fetch counters for',
      required: true,
    }),
  },
  async run(context) {
    const response = await httpClient.sendRequest({
      method: HttpMethod.GET,
      url: `https://eth.blockscout.com/api/v2/addresses/${context.propsValue.addressHash}/counters`,
    });
    if (response.status !== 200) {
      throw new Error(`Blockscout API request failed with status ${response.status}`);
    }
    return response.body;
  },
});
