/**
 * Payment Processing Tools
 */

import { z } from 'zod';
import type { TouchBistroApiClient } from '../lib/api-client.js';

export const paymentTools = {
  touchbistro_process_payment: {
    description: 'Process a payment for an order (full, partial, or split).',
    parameters: z.object({
      orderId: z.string().describe('Order ID'),
      type: z.enum(['full', 'partial', 'split']).describe('Payment type'),
      method: z
        .enum(['cash', 'credit_card', 'debit_card', 'gift_card', 'mobile_payment', 'check', 'house_account'])
        .describe('Payment method'),
      amount: z.number().describe('Payment amount'),
      tip: z.number().optional().describe('Tip amount'),
      cardLast4: z.string().optional().describe('Last 4 digits of card'),
      cardBrand: z.string().optional().describe('Card brand (Visa, Mastercard, etc.)'),
    }),
    handler: async (params: any, client: TouchBistroApiClient) => {
      const { orderId, ...payment } = params;
      const response = await client.processPayment(orderId, payment);

      if (!response.success) {
        throw new Error(response.error?.message || 'Failed to process payment');
      }

      return {
        content: [
          {
            type: 'text',
            text: `Payment processed successfully!\n\n${JSON.stringify(response.data, null, 2)}`,
          },
        ],
      };
    },
  },

  touchbistro_get_payments: {
    description: 'Get all payments for an order.',
    parameters: z.object({
      orderId: z.string().describe('Order ID'),
    }),
    handler: async (params: any, client: TouchBistroApiClient) => {
      const response = await client.getPayments(params.orderId);

      if (!response.success) {
        throw new Error(response.error?.message || 'Failed to fetch payments');
      }

      return {
        content: [
          {
            type: 'text',
            text: JSON.stringify(response.data, null, 2),
          },
        ],
      };
    },
  },

  touchbistro_refund_payment: {
    description: 'Refund a payment (full or partial).',
    parameters: z.object({
      paymentId: z.string().describe('Payment ID'),
      amount: z.number().optional().describe('Refund amount (full refund if not specified)'),
    }),
    handler: async (params: any, client: TouchBistroApiClient) => {
      const response = await client.refundPayment(params.paymentId, params.amount);

      if (!response.success) {
        throw new Error(response.error?.message || 'Failed to refund payment');
      }

      return {
        content: [
          {
            type: 'text',
            text: `Payment refunded!\n\n${JSON.stringify(response.data, null, 2)}`,
          },
        ],
      };
    },
  },

  touchbistro_void_payment: {
    description: 'Void a payment transaction.',
    parameters: z.object({
      paymentId: z.string().describe('Payment ID'),
    }),
    handler: async (params: any, client: TouchBistroApiClient) => {
      const response = await client.voidPayment(params.paymentId);

      if (!response.success) {
        throw new Error(response.error?.message || 'Failed to void payment');
      }

      return {
        content: [
          {
            type: 'text',
            text: `Payment voided!\n\n${JSON.stringify(response.data, null, 2)}`,
          },
        ],
      };
    },
  },
};
