import { z } from 'zod';
import { StripeClient } from '../clients/stripe.js';
import { Refund } from '../types/index.js';

const metadataSchema = z.record(z.string()).optional().describe('Set of key-value pairs for storing additional information');

export default [
  {
    name: 'stripe_list_refunds',
    description: 'List all refunds with optional filtering and pagination',
    inputSchema: z.object({
      limit: z.number().min(1).max(100).optional().describe('Number of refunds to return (1-100)'),
      starting_after: z.string().optional().describe('Cursor for pagination'),
      ending_before: z.string().optional().describe('Cursor for pagination'),
      charge: z.string().optional().describe('Filter by charge ID'),
      payment_intent: z.string().optional().describe('Filter by payment intent ID'),
      created: z.union([
        z.number(),
        z.object({
          gt: z.number().optional(),
          gte: z.number().optional(),
          lt: z.number().optional(),
          lte: z.number().optional(),
        })
      ]).optional().describe('Filter by creation timestamp or range'),
    }),
    handler: async (client: StripeClient, args: any) => {
      return await client.list<Refund>('/refunds', args);
    }
  },
  {
    name: 'stripe_get_refund',
    description: 'Retrieve a specific refund by ID',
    inputSchema: z.object({
      refund_id: z.string().describe('The ID of the refund to retrieve'),
      expand: z.array(z.string()).optional().describe('Fields to expand (e.g., ["charge"])')
    }),
    handler: async (client: StripeClient, args: any) => {
      const { refund_id, expand } = args;
      const params = expand ? { expand } : undefined;
      return await client.retrieve<Refund>('/refunds', refund_id, params);
    }
  },
  {
    name: 'stripe_create_refund',
    description: 'Create a refund for a charge or payment intent',
    inputSchema: z.object({
      charge: z.string().optional().describe('Charge ID to refund (use charge or payment_intent, not both)'),
      payment_intent: z.string().optional().describe('Payment intent ID to refund'),
      amount: z.number().optional().describe('Amount to refund in cents (defaults to full charge amount)'),
      reason: z.enum(['duplicate', 'fraudulent', 'requested_by_customer']).optional().describe('Reason for refund'),
      refund_application_fee: z.boolean().optional().describe('Whether to refund the application fee'),
      reverse_transfer: z.boolean().optional().describe('Whether to reverse the transfer'),
      metadata: metadataSchema
    }),
    handler: async (client: StripeClient, args: any) => {
      return await client.create<Refund>('/refunds', args, {
        idempotencyKey: client.generateIdempotencyKey()
      });
    }
  },
  {
    name: 'stripe_update_refund',
    description: 'Update a refund (limited fields can be updated)',
    inputSchema: z.object({
      refund_id: z.string().describe('The ID of the refund to update'),
      metadata: metadataSchema
    }),
    handler: async (client: StripeClient, args: any) => {
      const { refund_id, ...data } = args;
      return await client.update<Refund>('/refunds', refund_id, data);
    }
  },
  {
    name: 'stripe_cancel_refund',
    description: 'Cancel a refund that is in pending status',
    inputSchema: z.object({
      refund_id: z.string().describe('The ID of the refund to cancel')
    }),
    handler: async (client: StripeClient, args: any) => {
      return await client.post<Refund>(`/refunds/${args.refund_id}/cancel`, {});
    }
  }
];
