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

export function createRefundsTools(client: CloverClient) {
  return {
    clover_list_refunds: {
      description: 'List all refunds',
      inputSchema: {
        type: 'object',
        properties: {
          filter: {
            type: 'string',
            description: 'Filter expression (e.g., "createdTime>1234567890")',
          },
          limit: {
            type: 'number',
            description: 'Maximum number of refunds to return',
          },
        },
      },
      handler: async (args: any) => {
        const refunds = await client.fetchPaginated<CloverRefund>(
          '/refunds',
          { filter: args.filter },
          args.limit
        );
        return { refunds, count: refunds.length };
      },
    },

    clover_get_refund: {
      description: 'Get a specific refund by ID',
      inputSchema: {
        type: 'object',
        properties: {
          refundId: { type: 'string', description: 'Refund ID' },
        },
        required: ['refundId'],
      },
      handler: async (args: any) => {
        return await client.get<CloverRefund>(`/refunds/${args.refundId}`);
      },
    },

    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 to refund' },
          amount: { type: 'number', description: 'Refund amount in cents' },
          fullRefund: {
            type: 'boolean',
            description: 'Whether this is a full refund',
          },
        },
        required: ['orderId', 'paymentId', 'amount'],
      },
      handler: async (args: any) => {
        return await client.post<CloverRefund>('/refunds', {
          orderRef: { id: args.orderId },
          payment: { id: args.paymentId },
          amount: args.amount,
          fullRefund: args.fullRefund,
        });
      },
    },

    clover_list_order_refunds: {
      description: 'List refunds for a specific order',
      inputSchema: {
        type: 'object',
        properties: {
          orderId: { type: 'string', description: 'Order ID' },
        },
        required: ['orderId'],
      },
      handler: async (args: any) => {
        return await client.get<PaginatedResponse<CloverRefund>>(
          `/orders/${args.orderId}/refunds`
        );
      },
    },

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