import { z } from 'zod';
import { AcuityClient } from '../clients/acuity.js';

export function createCouponsTools(client: AcuityClient) {
  return {
    acuity_list_coupons: {
      description: 'List all coupons',
      inputSchema: z.object({}),
      handler: async () => {
        const coupons = await client.listCoupons();
        return {
          content: [{
            type: 'text',
            text: JSON.stringify(coupons, null, 2)
          }]
        };
      }
    },

    acuity_get_coupon: {
      description: 'Get a specific coupon by ID',
      inputSchema: z.object({
        id: z.number().describe('Coupon ID')
      }),
      handler: async (args: any) => {
        const coupon = await client.getCoupon(args.id);
        return {
          content: [{
            type: 'text',
            text: JSON.stringify(coupon, null, 2)
          }]
        };
      }
    },

    acuity_create_coupon: {
      description: 'Create a new coupon',
      inputSchema: z.object({
        code: z.string().describe('Coupon code'),
        name: z.string().optional().describe('Coupon name'),
        amountOff: z.number().optional().describe('Fixed amount discount'),
        percentageOff: z.number().optional().describe('Percentage discount (0-100)'),
        validFrom: z.string().optional().describe('Valid from date (YYYY-MM-DD)'),
        validTo: z.string().optional().describe('Valid to date (YYYY-MM-DD)'),
        maxUses: z.number().optional().describe('Maximum number of uses')
      }),
      handler: async (args: any) => {
        const coupon = await client.createCoupon(args);
        return {
          content: [{
            type: 'text',
            text: JSON.stringify(coupon, null, 2)
          }]
        };
      }
    },

    acuity_update_coupon: {
      description: 'Update an existing coupon',
      inputSchema: z.object({
        id: z.number().describe('Coupon ID'),
        code: z.string().optional(),
        name: z.string().optional(),
        amountOff: z.number().optional(),
        percentageOff: z.number().optional(),
        validFrom: z.string().optional(),
        validTo: z.string().optional(),
        maxUses: z.number().optional()
      }),
      handler: async (args: any) => {
        const { id, ...data } = args;
        const coupon = await client.updateCoupon(id, data);
        return {
          content: [{
            type: 'text',
            text: JSON.stringify(coupon, null, 2)
          }]
        };
      }
    },

    acuity_delete_coupon: {
      description: 'Delete a coupon',
      inputSchema: z.object({
        id: z.number().describe('Coupon ID')
      }),
      handler: async (args: any) => {
        await client.deleteCoupon(args.id);
        return {
          content: [{
            type: 'text',
            text: JSON.stringify({ success: true, message: 'Coupon deleted' })
          }]
        };
      }
    }
  };
}
