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

export const getAddressTransactions = createAction({
  name: 'get_address_transactions',
  displayName: 'Get Address Transactions',
  description: 'Get list of transactions for an address',
  audience: 'both',
  aiMetadata: { description: 'List the top-level transactions in which an address was the sender or receiver. Pick this for an account’s normal transaction history; use Get Address Internal Transactions for contract-internal value moves or Get Address Token Transfers for token movements. 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 transactions 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}/transactions`,
    });
    if (response.status !== 200) {
      throw new Error(`Blockscout API request failed with status ${response.status}`);
    }
    return response.body;
  },
});
