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

export const postTransaction = createAction({
 auth:PieceAuth.None(),
  name: 'post_transaction',
  displayName: 'Post Transaction',
  description: 'Submit a raw transaction to the network',
  audience: 'both',
  aiMetadata: { description: 'Broadcast a signed raw Bitcoin transaction (hex) to the network for inclusion in the mempool. This is the only write action here and is not idempotent: re-submitting may error or have real on-chain effects, so call it once with a fully signed transaction. Returns the resulting transaction ID.', idempotent: false },
  // category: 'Transactions',
  props: {
    rawTx: Property.LongText({
      displayName: 'Raw Transaction',
      description: 'The raw transaction in hex format to broadcast',
      required: true,
    }),
  },
  async run({ propsValue }) {
    const response = await httpClient.sendRequest({
      method: HttpMethod.POST,
      url: `${MEMPOOL_API_BASE_URL}/api/tx`,
      body: propsValue.rawTx,
    });
    return response.body;
  },
});
