import type { PaymentIntentResponse, RetrievePaymentIntentParams } from '@/tools/stripe/types'
import {
  PAYMENT_INTENT_METADATA_OUTPUT_PROPERTIES,
  PAYMENT_INTENT_OUTPUT,
} from '@/tools/stripe/types'
import type { ToolConfig } from '@/tools/types'

export const stripeRetrievePaymentIntentTool: ToolConfig<
  RetrievePaymentIntentParams,
  PaymentIntentResponse
> = {
  id: 'stripe_retrieve_payment_intent',
  name: 'Stripe Retrieve Payment Intent',
  description: 'Retrieve an existing Payment Intent by ID',
  version: '1.0.0',

  params: {
    apiKey: {
      type: 'string',
      required: true,
      visibility: 'user-only',
      description: 'Stripe API key (secret key)',
    },
    id: {
      type: 'string',
      required: true,
      visibility: 'user-or-llm',
      description: 'Payment Intent ID (e.g., pi_1234567890)',
    },
  },

  request: {
    url: (params) => `https://api.stripe.com/v1/payment_intents/${params.id}`,
    method: 'GET',
    headers: (params) => ({
      Authorization: `Bearer ${params.apiKey}`,
      'Content-Type': 'application/x-www-form-urlencoded',
    }),
  },

  transformResponse: async (response) => {
    const data = await response.json()
    return {
      success: true,
      output: {
        payment_intent: data,
        metadata: {
          id: data.id,
          status: data.status,
          amount: data.amount,
          currency: data.currency,
        },
      },
    }
  },

  outputs: {
    payment_intent: {
      ...PAYMENT_INTENT_OUTPUT,
      description: 'The retrieved Payment Intent object',
    },
    metadata: {
      type: 'json',
      description: 'Payment Intent metadata including ID, status, amount, and currency',
      properties: PAYMENT_INTENT_METADATA_OUTPUT_PROPERTIES,
    },
  },
}
