/**
 * Intercom Help Center Tools
 * Includes collections and sections
 */

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

// Zod schemas
const CreateCollectionSchema = z.object({
  name: z.string(),
  description: z.string().optional(),
  parent_id: z.string().optional(),
});

const UpdateCollectionSchema = z.object({
  id: z.string(),
  name: z.string().optional(),
  description: z.string().optional(),
});

const CreateSectionSchema = z.object({
  collection_id: z.string(),
  name: z.string(),
});

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

    {
      definition: {
        name: 'intercom_get_help_center',
        description: 'Retrieve a specific help center by ID.',
        inputSchema: {
          type: 'object',
          properties: {
            id: {
              type: 'string',
              description: 'Help center ID',
            },
          },
          required: ['id'],
        },
      },
      handler: async (args) => {
        const { id } = args as { id: string };
        return client.getHelpCenter(id);
      },
    },

    {
      definition: {
        name: 'intercom_list_collections',
        description: 'List all help center collections with pagination.',
        inputSchema: {
          type: 'object',
          properties: {
            per_page: {
              type: 'number',
              description: 'Number of results per page',
            },
            page: {
              type: 'number',
              description: 'Page number',
            },
          },
        },
      },
      handler: async (args) => {
        const params = args as { per_page?: number; page?: number };
        return client.listCollections(params);
      },
    },

    {
      definition: {
        name: 'intercom_get_collection',
        description: 'Retrieve a specific collection by ID.',
        inputSchema: {
          type: 'object',
          properties: {
            id: {
              type: 'string',
              description: 'Collection ID',
            },
          },
          required: ['id'],
        },
      },
      handler: async (args) => {
        const { id } = args as { id: string };
        return client.getCollection(id as any);
      },
    },

    {
      definition: {
        name: 'intercom_create_collection',
        description: 'Create a new help center collection.',
        inputSchema: {
          type: 'object',
          properties: {
            name: {
              type: 'string',
              description: 'Collection name (required)',
            },
            description: {
              type: 'string',
              description: 'Collection description',
            },
            parent_id: {
              type: 'string',
              description: 'Parent collection ID (for nested collections)',
            },
          },
          required: ['name'],
        },
      },
      handler: async (args) => {
        const data = CreateCollectionSchema.parse(args);
        return client.createCollection(data as any);
      },
    },

    {
      definition: {
        name: 'intercom_update_collection',
        description: 'Update an existing collection by ID.',
        inputSchema: {
          type: 'object',
          properties: {
            id: {
              type: 'string',
              description: 'Collection ID',
            },
            name: {
              type: 'string',
              description: 'Collection name',
            },
            description: {
              type: 'string',
              description: 'Collection description',
            },
          },
          required: ['id'],
        },
      },
      handler: async (args) => {
        const { id, ...data } = UpdateCollectionSchema.parse(args);
        return client.updateCollection(id as any, data as any);
      },
    },

    {
      definition: {
        name: 'intercom_delete_collection',
        description: 'Permanently delete a collection by ID. This action cannot be undone.',
        inputSchema: {
          type: 'object',
          properties: {
            id: {
              type: 'string',
              description: 'Collection ID to delete',
            },
          },
          required: ['id'],
        },
      },
      handler: async (args) => {
        const { id } = args as { id: string };
        return client.deleteCollection(id as any);
      },
    },

    {
      definition: {
        name: 'intercom_list_sections',
        description: 'List all sections within a collection.',
        inputSchema: {
          type: 'object',
          properties: {
            collection_id: {
              type: 'string',
              description: 'Collection ID',
            },
          },
          required: ['collection_id'],
        },
      },
      handler: async (args) => {
        const { collection_id } = args as { collection_id: string };
        return client.listSections(collection_id as any);
      },
    },

    {
      definition: {
        name: 'intercom_get_section',
        description: 'Retrieve a specific section by ID.',
        inputSchema: {
          type: 'object',
          properties: {
            id: {
              type: 'string',
              description: 'Section ID',
            },
          },
          required: ['id'],
        },
      },
      handler: async (args) => {
        const { id } = args as { id: string };
        return client.getSection(id as any);
      },
    },

    {
      definition: {
        name: 'intercom_create_section',
        description: 'Create a new section within a collection.',
        inputSchema: {
          type: 'object',
          properties: {
            collection_id: {
              type: 'string',
              description: 'Parent collection ID (required)',
            },
            name: {
              type: 'string',
              description: 'Section name (required)',
            },
          },
          required: ['collection_id', 'name'],
        },
      },
      handler: async (args) => {
        const { collection_id, name } = CreateSectionSchema.parse(args);
        return client.createSection(collection_id as any, { name });
      },
    },
  ];
}
