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

export function createDevicesTools(client: CloverClient) {
  return {
    clover_list_devices: {
      description: 'List all devices registered to the merchant',
      inputSchema: {
        type: 'object',
        properties: {
          limit: {
            type: 'number',
            description: 'Maximum number of devices to return',
          },
        },
      },
      handler: async (args: any) => {
        const devices = await client.fetchPaginated<CloverDevice>(
          '/devices',
          {},
          args.limit
        );
        return { devices, count: devices.length };
      },
    },

    clover_get_device: {
      description: 'Get a specific device by ID',
      inputSchema: {
        type: 'object',
        properties: {
          deviceId: { type: 'string', description: 'Device ID' },
        },
        required: ['deviceId'],
      },
      handler: async (args: any) => {
        return await client.get<CloverDevice>(`/devices/${args.deviceId}`);
      },
    },

    clover_get_device_by_serial: {
      description: 'Get device information by serial number',
      inputSchema: {
        type: 'object',
        properties: {
          serial: { type: 'string', description: 'Device serial number' },
        },
        required: ['serial'],
      },
      handler: async (args: any) => {
        const devices = await client.fetchPaginated<CloverDevice>('/devices', {
          filter: `serial=${args.serial}`,
        });
        
        if (devices.length === 0) {
          throw new Error(`Device with serial ${args.serial} not found`);
        }
        
        return devices[0];
      },
    },

    clover_list_device_payments: {
      description: 'List payments processed by a specific device',
      inputSchema: {
        type: 'object',
        properties: {
          deviceId: { type: 'string', description: 'Device ID' },
          limit: { type: 'number', description: 'Maximum number to return' },
        },
        required: ['deviceId'],
      },
      handler: async (args: any) => {
        const payments = await client.fetchPaginated<any>(
          '/payments',
          { filter: `device.id=${args.deviceId}` },
          args.limit
        );
        return { payments, count: payments.length };
      },
    },

    clover_open_cash_drawer: {
      description: 'Send command to open cash drawer on a device',
      inputSchema: {
        type: 'object',
        properties: {
          deviceId: { type: 'string', description: 'Device ID' },
          reason: { type: 'string', description: 'Reason for opening drawer' },
        },
        required: ['deviceId'],
      },
      handler: async (args: any) => {
        // Note: This would typically use Clover's device connector API
        return await client.post(`/devices/${args.deviceId}/open_cash_drawer`, {
          reason: args.reason,
        });
      },
    },

    clover_print_receipt: {
      description: 'Print a receipt on a device',
      inputSchema: {
        type: 'object',
        properties: {
          deviceId: { type: 'string', description: 'Device ID' },
          orderId: { type: 'string', description: 'Order ID to print receipt for' },
        },
        required: ['deviceId', 'orderId'],
      },
      handler: async (args: any) => {
        return await client.post(`/devices/${args.deviceId}/print`, {
          orderId: args.orderId,
        });
      },
    },
  };
}
