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

export function createAppointmentsTools(client: AcuityClient) {
  return {
    acuity_list_appointments: {
      description: 'List appointments with optional filters',
      inputSchema: z.object({
        minDate: z.string().optional().describe('Minimum date (YYYY-MM-DD)'),
        maxDate: z.string().optional().describe('Maximum date (YYYY-MM-DD)'),
        calendarID: z.number().optional().describe('Filter by calendar ID'),
        canceled: z.boolean().optional().describe('Include canceled appointments')
      }),
      handler: async (args: any) => {
        const appointments = await client.listAppointments(args);
        return {
          content: [{
            type: 'text',
            text: JSON.stringify(appointments, null, 2)
          }]
        };
      }
    },

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

    acuity_create_appointment: {
      description: 'Create a new appointment',
      inputSchema: z.object({
        appointmentTypeID: z.number().describe('Appointment type ID'),
        datetime: z.string().describe('Appointment datetime (ISO 8601)'),
        firstName: z.string().describe('Client first name'),
        lastName: z.string().describe('Client last name'),
        email: z.string().email().describe('Client email'),
        phone: z.string().optional().describe('Client phone'),
        calendarID: z.number().optional().describe('Calendar ID'),
        fields: z.array(z.object({
          id: z.number(),
          value: z.string()
        })).optional().describe('Form field values'),
        certificate: z.string().optional().describe('Gift certificate code')
      }),
      handler: async (args: any) => {
        const appointment = await client.createAppointment(args);
        return {
          content: [{
            type: 'text',
            text: JSON.stringify(appointment, null, 2)
          }]
        };
      }
    },

    acuity_update_appointment: {
      description: 'Update an existing appointment',
      inputSchema: z.object({
        id: z.number().describe('Appointment ID'),
        firstName: z.string().optional(),
        lastName: z.string().optional(),
        email: z.string().email().optional(),
        phone: z.string().optional(),
        notes: z.string().optional()
      }),
      handler: async (args: any) => {
        const { id, ...data } = args;
        const appointment = await client.updateAppointment(id, data);
        return {
          content: [{
            type: 'text',
            text: JSON.stringify(appointment, null, 2)
          }]
        };
      }
    },

    acuity_cancel_appointment: {
      description: 'Cancel an appointment',
      inputSchema: z.object({
        id: z.number().describe('Appointment ID'),
        noEmail: z.boolean().optional().describe('Skip sending cancellation email')
      }),
      handler: async (args: any) => {
        const { id, noEmail } = args;
        const appointment = await client.cancelAppointment(id, { noEmail });
        return {
          content: [{
            type: 'text',
            text: JSON.stringify(appointment, null, 2)
          }]
        };
      }
    },

    acuity_reschedule_appointment: {
      description: 'Reschedule an appointment to a new datetime',
      inputSchema: z.object({
        id: z.number().describe('Appointment ID'),
        datetime: z.string().describe('New datetime (ISO 8601)')
      }),
      handler: async (args: any) => {
        const appointment = await client.rescheduleAppointment(args.id, args.datetime);
        return {
          content: [{
            type: 'text',
            text: JSON.stringify(appointment, null, 2)
          }]
        };
      }
    },

    acuity_list_appointment_types: {
      description: 'List all appointment types',
      inputSchema: z.object({}),
      handler: async () => {
        const types = await client.listAppointmentTypes();
        return {
          content: [{
            type: 'text',
            text: JSON.stringify(types, null, 2)
          }]
        };
      }
    },

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