/**
 * Intercom Data Events Tools
 */

import { z } from 'zod';
import { Tool } from '@modelcontextprotocol/sdk/types.js';
import type { IntercomClient } from '../clients/intercom.js';

// Zod schemas
const SubmitEventSchema = z.object({
  event_name: z.string(),
  created_at: z.number().optional(),
  user_id: z.string().optional(),
  id: z.string().optional(),
  email: z.string().email().optional(),
  metadata: z.record(z.union([z.string(), z.number(), z.boolean()])).optional(),
});

const ListEventSummariesSchema = z.object({
  user_id: z.string().optional(),
  email: z.string().email().optional(),
  type: z.enum(['user', 'company']).optional(),
  count: z.number().optional(),
});

// Tool definitions
export function getTools(client: IntercomClient): Array<{
  definition: Tool;
  handler: (args: Record<string, unknown>) => Promise<unknown>;
}> {
  return [
    {
      definition: {
        name: 'intercom_submit_event',
        description: 'Submit a data event for a user or lead. Events track user actions and behaviors.',
        inputSchema: {
          type: 'object',
          properties: {
            event_name: {
              type: 'string',
              description: 'Event name (required) - e.g., "purchased-item", "logged-in"',
            },
            created_at: {
              type: 'number',
              description: 'Unix timestamp when event occurred (optional, defaults to now)',
            },
            user_id: {
              type: 'string',
              description: 'External user ID',
            },
            id: {
              type: 'string',
              description: 'Intercom contact ID',
            },
            email: {
              type: 'string',
              description: 'User email address',
            },
            metadata: {
              type: 'object',
              description: 'Custom event metadata (key-value pairs)',
            },
          },
          required: ['event_name'],
        },
      },
      handler: async (args) => {
        const data = SubmitEventSchema.parse(args);
        return client.submitEvent(data as any);
      },
    },

    {
      definition: {
        name: 'intercom_list_event_summaries',
        description: 'List event summaries for a specific user or company. Shows event counts and last occurrence.',
        inputSchema: {
          type: 'object',
          properties: {
            user_id: {
              type: 'string',
              description: 'External user ID',
            },
            email: {
              type: 'string',
              description: 'User email',
            },
            type: {
              type: 'string',
              enum: ['user', 'company'],
              description: 'Entity type',
            },
            count: {
              type: 'number',
              description: 'Number of event summaries to return',
            },
          },
        },
      },
      handler: async (args) => {
        const params = ListEventSummariesSchema.parse(args);
        return client.listEventSummaries(params as any);
      },
    },
  ];
}
