/**
 * Intercom Messages Tools
 * Supports in-app, email, and push messages
 */

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

// Zod schemas
const SendMessageSchema = z.object({
  message_type: z.enum(['inapp', 'email', 'push']),
  subject: z.string().optional(),
  body: z.string(),
  template: z.enum(['plain', 'personal']).optional(),
  from: z.object({
    type: z.literal('admin'),
    id: z.string(),
  }).optional(),
  to: z.object({
    type: z.enum(['contact', 'user', 'lead']),
    id: z.string().optional(),
    user_id: z.string().optional(),
    email: z.string().email().optional(),
  }).optional(),
  create_conversation_without_contact_reply: z.boolean().optional(),
});

// Tool definitions
export function getTools(client: IntercomClient): Array<{
  definition: Tool;
  handler: (args: Record<string, unknown>) => Promise<unknown>;
}> {
  return [
    {
      definition: {
        name: 'intercom_send_message',
        description: 'Send a message to a user, lead, or contact. Supports in-app, email, and push notifications.',
        inputSchema: {
          type: 'object',
          properties: {
            message_type: {
              type: 'string',
              enum: ['inapp', 'email', 'push'],
              description: 'Type of message to send',
            },
            subject: {
              type: 'string',
              description: 'Message subject (for email)',
            },
            body: {
              type: 'string',
              description: 'Message body (required)',
            },
            template: {
              type: 'string',
              enum: ['plain', 'personal'],
              description: 'Email template style',
            },
            from: {
              type: 'object',
              description: 'Sender (admin)',
              properties: {
                type: {
                  type: 'string',
                  enum: ['admin'],
                  description: 'Must be "admin"',
                },
                id: {
                  type: 'string',
                  description: 'Admin ID',
                },
              },
              required: ['type', 'id'],
            },
            to: {
              type: 'object',
              description: 'Recipient',
              properties: {
                type: {
                  type: 'string',
                  enum: ['contact', 'user', 'lead'],
                  description: 'Recipient type',
                },
                id: {
                  type: 'string',
                  description: 'Contact/lead ID (if type is contact)',
                },
                user_id: {
                  type: 'string',
                  description: 'External user ID',
                },
                email: {
                  type: 'string',
                  description: 'Recipient email',
                },
              },
              required: ['type'],
            },
            create_conversation_without_contact_reply: {
              type: 'boolean',
              description: 'Whether to create a conversation even if contact does not reply',
            },
          },
          required: ['message_type', 'body'],
        },
      },
      handler: async (args) => {
        const data = SendMessageSchema.parse(args);
        return client.sendMessage(data as any);
      },
    },

    {
      definition: {
        name: 'intercom_send_inapp_message',
        description: 'Send an in-app message to a contact. Shortcut for send_message with message_type=inapp.',
        inputSchema: {
          type: 'object',
          properties: {
            body: {
              type: 'string',
              description: 'Message body (required)',
            },
            to_contact_id: {
              type: 'string',
              description: 'Contact ID',
            },
            to_user_id: {
              type: 'string',
              description: 'External user ID',
            },
            to_email: {
              type: 'string',
              description: 'Contact email',
            },
            from_admin_id: {
              type: 'string',
              description: 'Admin ID sending the message',
            },
          },
          required: ['body'],
        },
      },
      handler: async (args) => {
        const { body, to_contact_id, to_user_id, to_email, from_admin_id } = args as any;
        
        const message: any = {
          message_type: 'inapp',
          body,
        };

        if (from_admin_id) {
          message.from = { type: 'admin', id: from_admin_id };
        }

        const to: any = {};
        if (to_contact_id) {
          to.type = 'contact';
          to.id = to_contact_id;
        } else if (to_user_id) {
          to.type = 'user';
          to.user_id = to_user_id;
        } else if (to_email) {
          to.type = 'contact';
          to.email = to_email;
        }

        if (Object.keys(to).length > 0) {
          message.to = to;
        }

        return client.sendMessage(message);
      },
    },

    {
      definition: {
        name: 'intercom_send_email_message',
        description: 'Send an email message to a contact. Shortcut for send_message with message_type=email.',
        inputSchema: {
          type: 'object',
          properties: {
            subject: {
              type: 'string',
              description: 'Email subject',
            },
            body: {
              type: 'string',
              description: 'Email body (required)',
            },
            template: {
              type: 'string',
              enum: ['plain', 'personal'],
              description: 'Email template',
            },
            to_contact_id: {
              type: 'string',
              description: 'Contact ID',
            },
            to_user_id: {
              type: 'string',
              description: 'External user ID',
            },
            to_email: {
              type: 'string',
              description: 'Contact email',
            },
            from_admin_id: {
              type: 'string',
              description: 'Admin ID sending the email',
            },
          },
          required: ['body'],
        },
      },
      handler: async (args) => {
        const { body, subject, template, to_contact_id, to_user_id, to_email, from_admin_id } = args as any;
        
        const message: any = {
          message_type: 'email',
          body,
        };

        if (subject) message.subject = subject;
        if (template) message.template = template;

        if (from_admin_id) {
          message.from = { type: 'admin', id: from_admin_id };
        }

        const to: any = {};
        if (to_contact_id) {
          to.type = 'contact';
          to.id = to_contact_id;
        } else if (to_user_id) {
          to.type = 'user';
          to.user_id = to_user_id;
        } else if (to_email) {
          to.type = 'contact';
          to.email = to_email;
        }

        if (Object.keys(to).length > 0) {
          message.to = to;
        }

        return client.sendMessage(message);
      },
    },

    {
      definition: {
        name: 'intercom_send_push_message',
        description: 'Send a push notification to a contact. Shortcut for send_message with message_type=push.',
        inputSchema: {
          type: 'object',
          properties: {
            body: {
              type: 'string',
              description: 'Push notification body (required)',
            },
            to_contact_id: {
              type: 'string',
              description: 'Contact ID',
            },
            to_user_id: {
              type: 'string',
              description: 'External user ID',
            },
            to_email: {
              type: 'string',
              description: 'Contact email',
            },
            from_admin_id: {
              type: 'string',
              description: 'Admin ID sending the push',
            },
          },
          required: ['body'],
        },
      },
      handler: async (args) => {
        const { body, to_contact_id, to_user_id, to_email, from_admin_id } = args as any;
        
        const message: any = {
          message_type: 'push',
          body,
        };

        if (from_admin_id) {
          message.from = { type: 'admin', id: from_admin_id };
        }

        const to: any = {};
        if (to_contact_id) {
          to.type = 'contact';
          to.id = to_contact_id;
        } else if (to_user_id) {
          to.type = 'user';
          to.user_id = to_user_id;
        } else if (to_email) {
          to.type = 'contact';
          to.email = to_email;
        }

        if (Object.keys(to).length > 0) {
          message.to = to;
        }

        return client.sendMessage(message);
      },
    },
  ];
}
