import { CloverClient } from '../clients/clover.js';

export function createTipsTools(client: CloverClient) {
  return {
    clover_get_payment_tip: {
      description: 'Get tip information for a payment',
      inputSchema: {
        type: 'object',
        properties: {
          paymentId: { type: 'string', description: 'Payment ID' },
        },
        required: ['paymentId'],
      },
      handler: async (args: any) => {
        const payment = await client.get<any>(`/payments/${args.paymentId}`);
        return {
          paymentId: args.paymentId,
          tipAmount: payment.tipAmount || 0,
          amount: payment.amount,
          total: (payment.amount || 0) + (payment.tipAmount || 0),
        };
      },
    },

    clover_update_payment_tip: {
      description: 'Update tip amount for a payment',
      inputSchema: {
        type: 'object',
        properties: {
          paymentId: { type: 'string', description: 'Payment ID' },
          tipAmount: {
            type: 'number',
            description: 'Tip amount in cents',
          },
        },
        required: ['paymentId', 'tipAmount'],
      },
      handler: async (args: any) => {
        return await client.post(`/payments/${args.paymentId}`, {
          tipAmount: args.tipAmount,
        });
      },
    },

    clover_get_employee_tips: {
      description: 'Get tips collected by an employee during a shift or time period',
      inputSchema: {
        type: 'object',
        properties: {
          employeeId: { type: 'string', description: 'Employee ID' },
          startTime: {
            type: 'number',
            description: 'Start timestamp (Unix time in ms)',
          },
          endTime: {
            type: 'number',
            description: 'End timestamp (Unix time in ms)',
          },
        },
        required: ['employeeId'],
      },
      handler: async (args: any) => {
        // Fetch payments for the employee in the time range
        const filter = args.startTime && args.endTime
          ? `employee.id=${args.employeeId}&createdTime>=${args.startTime}&createdTime<=${args.endTime}`
          : `employee.id=${args.employeeId}`;

        const payments = await client.fetchPaginated<any>('/payments', { filter });
        
        const totalTips = payments.reduce((sum, p) => sum + (p.tipAmount || 0), 0);
        const paymentCount = payments.length;
        const averageTip = paymentCount > 0 ? totalTips / paymentCount : 0;

        return {
          employeeId: args.employeeId,
          totalTips,
          paymentCount,
          averageTip,
          startTime: args.startTime,
          endTime: args.endTime,
        };
      },
    },

    clover_get_shift_tips: {
      description: 'Get tips collected during a specific shift',
      inputSchema: {
        type: 'object',
        properties: {
          shiftId: { type: 'string', description: 'Shift ID' },
        },
        required: ['shiftId'],
      },
      handler: async (args: any) => {
        const shift = await client.get<any>(`/shifts/${args.shiftId}`);
        
        return {
          shiftId: args.shiftId,
          employeeId: shift.employee?.id,
          cashTipsCollected: shift.cashTipsCollected || 0,
          inTime: shift.inTime,
          outTime: shift.outTime,
        };
      },
    },

    clover_add_cash_tip_to_shift: {
      description: 'Add cash tips collected during a shift',
      inputSchema: {
        type: 'object',
        properties: {
          shiftId: { type: 'string', description: 'Shift ID' },
          cashTipsCollected: {
            type: 'number',
            description: 'Cash tips collected in cents',
          },
        },
        required: ['shiftId', 'cashTipsCollected'],
      },
      handler: async (args: any) => {
        return await client.post(`/shifts/${args.shiftId}`, {
          cashTipsCollected: args.cashTipsCollected,
        });
      },
    },
  };
}
