// Events Tools

import { CalendlyClient } from '../clients/calendly.js';

export function createEventsTools(client: CalendlyClient) {
  return {
    calendly_list_scheduled_events: {
      description: 'List scheduled events with filters (organization, user, status, date range)',
      parameters: {
        type: 'object',
        properties: {
          organization: {
            type: 'string',
            description: 'Organization URI to filter by',
          },
          user: {
            type: 'string',
            description: 'User URI to filter by',
          },
          invitee_email: {
            type: 'string',
            description: 'Filter by invitee email',
          },
          status: {
            type: 'string',
            enum: ['active', 'canceled'],
            description: 'Event status filter',
          },
          min_start_time: {
            type: 'string',
            description: 'Minimum start time (ISO 8601)',
          },
          max_start_time: {
            type: 'string',
            description: 'Maximum start time (ISO 8601)',
          },
          count: {
            type: 'number',
            description: 'Number of results per page (max 100)',
            default: 20,
          },
          page_token: {
            type: 'string',
            description: 'Pagination token',
          },
          sort: {
            type: 'string',
            description: 'Sort order (e.g., start_time:asc, start_time:desc)',
          },
        },
      },
      handler: async (args: any) => {
        const result = await client.listEvents(args);
        return {
          content: [
            {
              type: 'text',
              text: JSON.stringify(result, null, 2),
            },
          ],
        };
      },
    },

    calendly_get_event: {
      description: 'Get details of a specific scheduled event by UUID',
      parameters: {
        type: 'object',
        properties: {
          uuid: {
            type: 'string',
            description: 'Event UUID (from event URI)',
          },
        },
        required: ['uuid'],
      },
      handler: async (args: any) => {
        const result = await client.getEvent(args.uuid);
        return {
          content: [
            {
              type: 'text',
              text: JSON.stringify(result, null, 2),
            },
          ],
        };
      },
    },

    calendly_cancel_event: {
      description: 'Cancel a scheduled event',
      parameters: {
        type: 'object',
        properties: {
          uuid: {
            type: 'string',
            description: 'Event UUID to cancel',
          },
          reason: {
            type: 'string',
            description: 'Cancellation reason',
            default: 'Event canceled',
          },
        },
        required: ['uuid'],
      },
      handler: async (args: any) => {
        const result = await client.cancelEvent(args.uuid, args.reason);
        return {
          content: [
            {
              type: 'text',
              text: JSON.stringify(result, null, 2),
            },
          ],
        };
      },
    },

    calendly_list_event_invitees: {
      description: 'List invitees for a specific event',
      parameters: {
        type: 'object',
        properties: {
          event_uuid: {
            type: 'string',
            description: 'Event UUID',
          },
          count: {
            type: 'number',
            description: 'Number of results per page',
            default: 20,
          },
          page_token: {
            type: 'string',
            description: 'Pagination token',
          },
          sort: {
            type: 'string',
            description: 'Sort order',
          },
        },
        required: ['event_uuid'],
      },
      handler: async (args: any) => {
        const result = await client.listEventInvitees(args.event_uuid, {
          count: args.count,
          page_token: args.page_token,
          sort: args.sort,
        });
        return {
          content: [
            {
              type: 'text',
              text: JSON.stringify(result, null, 2),
            },
          ],
        };
      },
    },

    calendly_get_invitee: {
      description: 'Get details of a specific invitee',
      parameters: {
        type: 'object',
        properties: {
          invitee_uuid: {
            type: 'string',
            description: 'Invitee UUID',
          },
        },
        required: ['invitee_uuid'],
      },
      handler: async (args: any) => {
        const result = await client.getInvitee(args.invitee_uuid);
        return {
          content: [
            {
              type: 'text',
              text: JSON.stringify(result, null, 2),
            },
          ],
        };
      },
    },

    calendly_list_no_shows: {
      description: 'List no-shows for a specific invitee',
      parameters: {
        type: 'object',
        properties: {
          invitee_uri: {
            type: 'string',
            description: 'Invitee URI',
          },
        },
        required: ['invitee_uri'],
      },
      handler: async (args: any) => {
        const result = await client.listInviteeNoShows(args.invitee_uri);
        return {
          content: [
            {
              type: 'text',
              text: JSON.stringify(result, null, 2),
            },
          ],
        };
      },
    },

    calendly_mark_no_show: {
      description: 'Mark an invitee as a no-show',
      parameters: {
        type: 'object',
        properties: {
          invitee_uri: {
            type: 'string',
            description: 'Invitee URI to mark as no-show',
          },
        },
        required: ['invitee_uri'],
      },
      handler: async (args: any) => {
        const result = await client.createInviteeNoShow(args.invitee_uri);
        return {
          content: [
            {
              type: 'text',
              text: JSON.stringify(result, null, 2),
            },
          ],
        };
      },
    },

    calendly_unmark_no_show: {
      description: 'Remove no-show status from an invitee',
      parameters: {
        type: 'object',
        properties: {
          no_show_uuid: {
            type: 'string',
            description: 'No-show UUID to delete',
          },
        },
        required: ['no_show_uuid'],
      },
      handler: async (args: any) => {
        await client.deleteInviteeNoShow(args.no_show_uuid);
        return {
          content: [
            {
              type: 'text',
              text: JSON.stringify({ success: true, message: 'No-show removed' }),
            },
          ],
        };
      },
    },
  };
}
