import type { ChargeResponse, RetrieveChargeParams } from '@/tools/stripe/types'
import type { ToolConfig } from '@/tools/types'

export const stripeRetrieveChargeTool: ToolConfig<RetrieveChargeParams, ChargeResponse> = {
  id: 'stripe_retrieve_charge',
  name: 'Stripe Retrieve Charge',
  description: 'Retrieve an existing charge 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: 'Charge ID (e.g., ch_1234567890)',
    },
  },

  request: {
    url: (params) => `https://api.stripe.com/v1/charges/${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: {
        charge: data,
        metadata: {
          id: data.id,
          status: data.status,
          amount: data.amount,
          currency: data.currency,
          paid: data.paid,
        },
      },
    }
  },

  outputs: {
    charge: {
      type: 'json',
      description: 'The retrieved Charge object',
    },
    metadata: {
      type: 'json',
      description: 'Charge metadata including ID, status, amount, currency, and paid status',

      properties: {
        id: { type: 'string', description: 'Stripe unique identifier' },
        status: { type: 'string', description: 'Current state of the resource' },
        amount: { type: 'number', description: 'Amount in smallest currency unit (e.g., cents)' },
        currency: { type: 'string', description: 'Three-letter ISO currency code (lowercase)' },
        paid: { type: 'boolean', description: 'Whether payment has been received' },
      },
    },
  },
}
