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

export function createDiscountsTools(client: CloverClient) {
  return {
    clover_list_discounts: {
      description: 'List all discounts',
      inputSchema: {
        type: 'object',
        properties: {
          filter: {
            type: 'string',
            description: 'Filter expression',
          },
        },
      },
      handler: async (args: any) => {
        const discounts = await client.fetchPaginated<CloverDiscount>(
          '/discounts',
          { filter: args.filter }
        );
        return { discounts, count: discounts.length };
      },
    },

    clover_get_discount: {
      description: 'Get a specific discount by ID',
      inputSchema: {
        type: 'object',
        properties: {
          discountId: { type: 'string', description: 'Discount ID' },
        },
        required: ['discountId'],
      },
      handler: async (args: any) => {
        return await client.get<CloverDiscount>(`/discounts/${args.discountId}`);
      },
    },

    clover_create_discount: {
      description: 'Create a new discount',
      inputSchema: {
        type: 'object',
        properties: {
          name: { type: 'string', description: 'Discount name' },
          amount: {
            type: 'number',
            description: 'Fixed discount amount in cents (use this OR percentage)',
          },
          percentage: {
            type: 'number',
            description: 'Percentage discount (0-100, use this OR amount)',
          },
        },
        required: ['name'],
      },
      handler: async (args: any) => {
        return await client.post<CloverDiscount>('/discounts', {
          name: args.name,
          amount: args.amount,
          percentage: args.percentage,
        });
      },
    },

    clover_update_discount: {
      description: 'Update an existing discount',
      inputSchema: {
        type: 'object',
        properties: {
          discountId: { type: 'string', description: 'Discount ID' },
          name: { type: 'string', description: 'Discount name' },
          amount: {
            type: 'number',
            description: 'Fixed discount amount in cents',
          },
          percentage: {
            type: 'number',
            description: 'Percentage discount (0-100)',
          },
        },
        required: ['discountId'],
      },
      handler: async (args: any) => {
        const { discountId, ...updateData } = args;
        return await client.post<CloverDiscount>(`/discounts/${discountId}`, updateData);
      },
    },

    clover_delete_discount: {
      description: 'Delete a discount',
      inputSchema: {
        type: 'object',
        properties: {
          discountId: { type: 'string', description: 'Discount ID' },
        },
        required: ['discountId'],
      },
      handler: async (args: any) => {
        await client.delete(`/discounts/${args.discountId}`);
        return { success: true, discountId: args.discountId };
      },
    },
  };
}
