import { CloverClient } from '../clients/clover.js';
import { CloverPayment, CloverRefund } from '../types/index.js';

export function createPaymentsTools(client: CloverClient) {
  return {
    clover_list_payments: {
      description: 'List payments',
      inputSchema: {
        type: 'object',
        properties: {
          filter: {
            type: 'string',
            description: 'Filter expression (e.g., "result=SUCCESS")',
          },
          expand: {
            type: 'string',
            description: 'Comma-separated fields to expand (cardTransaction, refunds)',
          },
          limit: {
            type: 'number',
            description: 'Maximum number of payments to return',
          },
        },
      },
      handler: async (args: any) => {
        const payments = await client.fetchPaginated<CloverPayment>(
          '/payments',
          { filter: args.filter, expand: args.expand },
          args.limit
        );
        return { payments, count: payments.length };
      },
    },

    clover_get_payment: {
      description: 'Get a specific payment by ID',
      inputSchema: {
        type: 'object',
        properties: {
          paymentId: { type: 'string', description: 'Payment ID' },
          expand: {
            type: 'string',
            description: 'Comma-separated fields to expand',
          },
        },
        required: ['paymentId'],
      },
      handler: async (args: any) => {
        return await client.get<CloverPayment>(`/payments/${args.paymentId}`, {
          expand: args.expand,
        });
      },
    },

    clover_create_refund: {
      description: 'Create a refund for a payment',
      inputSchema: {
        type: 'object',
        properties: {
          orderId: { type: 'string', description: 'Order ID' },
          paymentId: { type: 'string', description: 'Payment ID' },
          amount: {
            type: 'number',
            description: 'Refund amount in cents',
          },
          fullRefund: {
            type: 'boolean',
            description: 'Full refund flag (default: false)',
          },
        },
        required: ['orderId', 'paymentId', 'amount'],
      },
      handler: async (args: any) => {
        return await client.post<CloverRefund>(
          `/orders/${args.orderId}/payments/${args.paymentId}/refunds`,
          {
            amount: args.amount,
            fullRefund: args.fullRefund,
          }
        );
      },
    },

    clover_list_refunds: {
      description: 'List refunds for a payment',
      inputSchema: {
        type: 'object',
        properties: {
          orderId: { type: 'string', description: 'Order ID' },
          paymentId: { type: 'string', description: 'Payment ID' },
        },
        required: ['orderId', 'paymentId'],
      },
      handler: async (args: any) => {
        return await client.get(
          `/orders/${args.orderId}/payments/${args.paymentId}/refunds`
        );
      },
    },
  };
}
