/**
 * Intercom Tags Tools
 */

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

// Zod schemas
const CreateTagSchema = z.object({
  name: z.string(),
});

const TagObjectSchema = z.object({
  tag_id: z.string(),
  contact_id: z.string().optional(),
  company_id: z.string().optional(),
});

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

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

    {
      definition: {
        name: 'intercom_create_tag',
        description: 'Create a new tag in your workspace.',
        inputSchema: {
          type: 'object',
          properties: {
            name: {
              type: 'string',
              description: 'Tag name (required)',
            },
          },
          required: ['name'],
        },
      },
      handler: async (args) => {
        const { name } = CreateTagSchema.parse(args);
        return client.createTag(name);
      },
    },

    {
      definition: {
        name: 'intercom_delete_tag',
        description: 'Permanently delete a tag by ID. This removes the tag from all tagged objects.',
        inputSchema: {
          type: 'object',
          properties: {
            id: {
              type: 'string',
              description: 'Tag ID to delete',
            },
          },
          required: ['id'],
        },
      },
      handler: async (args) => {
        const { id } = args as { id: string };
        return client.deleteTag(id as any);
      },
    },

    {
      definition: {
        name: 'intercom_tag_contact',
        description: 'Apply a tag to a contact.',
        inputSchema: {
          type: 'object',
          properties: {
            contact_id: {
              type: 'string',
              description: 'Contact ID',
            },
            tag_id: {
              type: 'string',
              description: 'Tag ID to apply',
            },
          },
          required: ['contact_id', 'tag_id'],
        },
      },
      handler: async (args) => {
        const { contact_id, tag_id } = args as { contact_id: string; tag_id: string };
        return client.tagContact(contact_id as any, tag_id as any);
      },
    },

    {
      definition: {
        name: 'intercom_untag_contact',
        description: 'Remove a tag from a contact.',
        inputSchema: {
          type: 'object',
          properties: {
            contact_id: {
              type: 'string',
              description: 'Contact ID',
            },
            tag_id: {
              type: 'string',
              description: 'Tag ID to remove',
            },
          },
          required: ['contact_id', 'tag_id'],
        },
      },
      handler: async (args) => {
        const { contact_id, tag_id } = args as { contact_id: string; tag_id: string };
        return client.untagContact(contact_id as any, tag_id as any);
      },
    },

    {
      definition: {
        name: 'intercom_tag_company',
        description: 'Apply a tag to a company.',
        inputSchema: {
          type: 'object',
          properties: {
            company_id: {
              type: 'string',
              description: 'Company ID',
            },
            tag_id: {
              type: 'string',
              description: 'Tag ID to apply',
            },
          },
          required: ['company_id', 'tag_id'],
        },
      },
      handler: async (args) => {
        const { company_id, tag_id } = args as { company_id: string; tag_id: string };
        return client.tagCompany(company_id as any, tag_id as any);
      },
    },

    {
      definition: {
        name: 'intercom_untag_company',
        description: 'Remove a tag from a company.',
        inputSchema: {
          type: 'object',
          properties: {
            company_id: {
              type: 'string',
              description: 'Company ID',
            },
            tag_id: {
              type: 'string',
              description: 'Tag ID to remove',
            },
          },
          required: ['company_id', 'tag_id'],
        },
      },
      handler: async (args) => {
        const { company_id, tag_id } = args as { company_id: string; tag_id: string };
        return client.untagCompany(company_id as any, tag_id as any);
      },
    },
  ];
}
