/**
 * Intercom Companies Tools
 */

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

// Zod schemas
const CreateCompanySchema = z.object({
  name: z.string(),
  company_id: z.string().optional(),
  website: z.string().url().optional(),
  plan: z.string().optional(),
  size: z.number().optional(),
  industry: z.string().optional(),
  remote_created_at: z.number().optional(),
  monthly_spend: z.number().optional(),
  custom_attributes: z.record(z.union([z.string(), z.number(), z.boolean()])).optional(),
});

const UpdateCompanySchema = z.object({
  id: z.string(),
  name: z.string().optional(),
  company_id: z.string().optional(),
  website: z.string().url().optional(),
  plan: z.string().optional(),
  size: z.number().optional(),
  industry: z.string().optional(),
  remote_created_at: z.number().optional(),
  monthly_spend: z.number().optional(),
  custom_attributes: z.record(z.union([z.string(), z.number(), z.boolean()])).optional(),
});

const AttachContactSchema = z.object({
  contact_id: z.string(),
  company_id: z.string(),
});

// Tool definitions
export function getTools(client: IntercomClient): Array<{
  definition: Tool;
  handler: (args: Record<string, unknown>) => Promise<unknown>;
}> {
  return [
    {
      definition: {
        name: 'intercom_list_companies',
        description: 'List all companies with cursor-based pagination.',
        inputSchema: {
          type: 'object',
          properties: {
            per_page: {
              type: 'number',
              description: 'Number of results per page (max 150)',
              maximum: 150,
            },
            starting_after: {
              type: 'string',
              description: 'Cursor for pagination',
            },
          },
        },
      },
      handler: async (args) => {
        const params = args as { per_page?: number; starting_after?: string };
        return client.listCompanies(params);
      },
    },

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

    {
      definition: {
        name: 'intercom_create_company',
        description: 'Create a new company in Intercom.',
        inputSchema: {
          type: 'object',
          properties: {
            name: {
              type: 'string',
              description: 'Company name (required)',
            },
            company_id: {
              type: 'string',
              description: 'Unique company identifier from your system',
            },
            website: {
              type: 'string',
              description: 'Company website URL',
            },
            plan: {
              type: 'string',
              description: 'Company plan/tier name',
            },
            size: {
              type: 'number',
              description: 'Number of employees',
            },
            industry: {
              type: 'string',
              description: 'Industry/sector',
            },
            remote_created_at: {
              type: 'number',
              description: 'Unix timestamp when company was created in your system',
            },
            monthly_spend: {
              type: 'number',
              description: 'Monthly spend/revenue',
            },
            custom_attributes: {
              type: 'object',
              description: 'Custom attributes object',
            },
          },
          required: ['name'],
        },
      },
      handler: async (args) => {
        const data = CreateCompanySchema.parse(args);
        return client.createCompany(data as any);
      },
    },

    {
      definition: {
        name: 'intercom_update_company',
        description: 'Update an existing company by ID.',
        inputSchema: {
          type: 'object',
          properties: {
            id: {
              type: 'string',
              description: 'Company ID',
            },
            name: {
              type: 'string',
              description: 'Company name',
            },
            company_id: {
              type: 'string',
              description: 'Unique company identifier',
            },
            website: {
              type: 'string',
              description: 'Company website',
            },
            plan: {
              type: 'string',
              description: 'Plan name',
            },
            size: {
              type: 'number',
              description: 'Number of employees',
            },
            industry: {
              type: 'string',
              description: 'Industry',
            },
            remote_created_at: {
              type: 'number',
              description: 'Creation timestamp',
            },
            monthly_spend: {
              type: 'number',
              description: 'Monthly spend',
            },
            custom_attributes: {
              type: 'object',
              description: 'Custom attributes',
            },
          },
          required: ['id'],
        },
      },
      handler: async (args) => {
        const { id, ...data } = UpdateCompanySchema.parse(args);
        return client.updateCompany(id as any, data as any);
      },
    },

    {
      definition: {
        name: 'intercom_scroll_companies',
        description: 'Scroll through all companies using scroll API. Better for large datasets than pagination.',
        inputSchema: {
          type: 'object',
          properties: {
            scroll_param: {
              type: 'string',
              description: 'Scroll parameter from previous response (omit for first page)',
            },
          },
        },
      },
      handler: async (args) => {
        const { scroll_param } = args as { scroll_param?: string };
        return client.scrollCompanies(scroll_param);
      },
    },

    {
      definition: {
        name: 'intercom_attach_contact_to_company',
        description: 'Attach a contact to a company. Creates the relationship between contact and company.',
        inputSchema: {
          type: 'object',
          properties: {
            contact_id: {
              type: 'string',
              description: 'Contact ID',
            },
            company_id: {
              type: 'string',
              description: 'Company ID',
            },
          },
          required: ['contact_id', 'company_id'],
        },
      },
      handler: async (args) => {
        const { contact_id, company_id } = AttachContactSchema.parse(args);
        return client.attachContactToCompany(contact_id as any, company_id as any);
      },
    },

    {
      definition: {
        name: 'intercom_detach_contact_from_company',
        description: 'Detach a contact from a company. Removes the relationship.',
        inputSchema: {
          type: 'object',
          properties: {
            contact_id: {
              type: 'string',
              description: 'Contact ID',
            },
            company_id: {
              type: 'string',
              description: 'Company ID',
            },
          },
          required: ['contact_id', 'company_id'],
        },
      },
      handler: async (args) => {
        const { contact_id, company_id } = AttachContactSchema.parse(args);
        return client.detachContactFromCompany(contact_id as any, company_id as any);
      },
    },
  ];
}
