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

export default [
  {
    name: 'stripe_get_balance',
    description: 'Retrieve the current account balance',
    inputSchema: z.object({
      expand: z.array(z.string()).optional().describe('Fields to expand')
    }),
    handler: async (client: StripeClient, args: any) => {
      return await client.get('/balance', args.expand ? { expand: args.expand } : undefined);
    }
  },
  {
    name: 'stripe_list_balance_transactions',
    description: 'List all balance transactions (history of changes to account balance)',
    inputSchema: z.object({
      limit: z.number().min(1).max(100).optional().describe('Number of transactions to return (1-100)'),
      starting_after: z.string().optional().describe('Cursor for pagination'),
      ending_before: z.string().optional().describe('Cursor for pagination'),
      available_on: 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 available_on timestamp or range'),
      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'),
      currency: z.string().optional().describe('Filter by currency (e.g., "usd")'),
      payout: z.string().optional().describe('Filter by payout ID'),
      source: z.string().optional().describe('Filter by source ID (charge, refund, etc.)'),
      type: z.enum([
        'charge',
        'refund',
        'adjustment',
        'application_fee',
        'application_fee_refund',
        'transfer',
        'payment',
        'payout',
        'payout_cancel',
        'payout_failure',
        'stripe_fee',
        'tax'
      ]).optional().describe('Filter by transaction type')
    }),
    handler: async (client: StripeClient, args: any) => {
      return await client.list<BalanceTransaction>('/balance_transactions', args);
    }
  },
  {
    name: 'stripe_get_balance_transaction',
    description: 'Retrieve a specific balance transaction by ID',
    inputSchema: z.object({
      transaction_id: z.string().describe('The ID of the balance transaction to retrieve'),
      expand: z.array(z.string()).optional().describe('Fields to expand (e.g., ["source"])')
    }),
    handler: async (client: StripeClient, args: any) => {
      const { transaction_id, expand } = args;
      const params = expand ? { expand } : undefined;
      return await client.retrieve<BalanceTransaction>('/balance_transactions', transaction_id, params);
    }
  }
];
