/**
 * Intercom Admins Tools
 */

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

// Zod schemas
const SetAwayModeSchema = z.object({
  admin_id: z.string(),
  away_mode_enabled: z.boolean(),
  away_mode_reassign: z.boolean().optional(),
});

// Tool definitions
export function getTools(client: IntercomClient): Array<{
  definition: Tool;
  handler: (args: Record<string, unknown>) => Promise<unknown>;
}> {
  return [
    {
      definition: {
        name: 'intercom_list_admins',
        description: 'List all admins (teammates) in your workspace.',
        inputSchema: {
          type: 'object',
          properties: {},
        },
      },
      handler: async () => {
        return client.listAdmins();
      },
    },

    {
      definition: {
        name: 'intercom_get_admin',
        description: 'Retrieve a specific admin by ID, including away mode status and team assignments.',
        inputSchema: {
          type: 'object',
          properties: {
            id: {
              type: 'string',
              description: 'Admin ID',
            },
          },
          required: ['id'],
        },
      },
      handler: async (args) => {
        const { id } = args as { id: string };
        return client.getAdmin(id as any);
      },
    },

    {
      definition: {
        name: 'intercom_set_admin_away',
        description: 'Set an admin\'s away mode status. When away, conversations can be automatically reassigned.',
        inputSchema: {
          type: 'object',
          properties: {
            admin_id: {
              type: 'string',
              description: 'Admin ID',
            },
            away_mode_enabled: {
              type: 'boolean',
              description: 'Enable or disable away mode',
            },
            away_mode_reassign: {
              type: 'boolean',
              description: 'Whether to automatically reassign conversations when away',
            },
          },
          required: ['admin_id', 'away_mode_enabled'],
        },
      },
      handler: async (args) => {
        const { admin_id, away_mode_enabled, away_mode_reassign } = SetAwayModeSchema.parse(args);
        
        // Note: The Intercom API doesn't have a direct "set away mode" endpoint in the client
        // This would typically be done via the admin update endpoint
        // For now, we'll throw an error indicating this needs to be implemented
        throw new Error('Setting admin away mode requires admin update endpoint - not yet implemented in client');
      },
    },
  ];
}
