/**
 * HubSpot Pipelines Tools
 * Tools for managing pipelines and pipeline stages (deals + tickets)
 */

import type { HubSpotClient } from '../clients/hubspot.js';

export function getTools(client: HubSpotClient) {
  return [
    // Deal Pipelines
    {
      name: 'hubspot_list_deal_pipelines',
      description: 'List all deal pipelines',
      inputSchema: {
        type: 'object',
        properties: {},
      },
      handler: async () => {
        const pipelines = await client.getPipelines('deals');

        return {
          content: [
            {
              type: 'text',
              text: JSON.stringify(
                {
                  total: pipelines.length,
                  pipelines,
                },
                null,
                2
              ),
            },
          ],
        };
      },
    },

    {
      name: 'hubspot_get_deal_pipeline',
      description: 'Get a specific deal pipeline by ID',
      inputSchema: {
        type: 'object',
        properties: {
          pipelineId: {
            type: 'string',
            description: 'Pipeline ID',
          },
        },
        required: ['pipelineId'],
      },
      handler: async (args: any) => {
        const pipeline = await client.apiRequest<any>({
          method: 'GET',
          url: `/crm/v3/pipelines/deals/${args.pipelineId}`,
        });

        return {
          content: [
            {
              type: 'text',
              text: JSON.stringify(pipeline, null, 2),
            },
          ],
        };
      },
    },

    {
      name: 'hubspot_create_deal_pipeline',
      description: 'Create a new deal pipeline',
      inputSchema: {
        type: 'object',
        properties: {
          label: {
            type: 'string',
            description: 'Pipeline label/name',
          },
          displayOrder: {
            type: 'number',
            description: 'Display order',
          },
          stages: {
            type: 'array',
            items: {
              type: 'object',
              properties: {
                label: { type: 'string' },
                displayOrder: { type: 'number' },
                metadata: { type: 'object' },
              },
            },
            description: 'Array of pipeline stages',
          },
        },
        required: ['label'],
      },
      handler: async (args: any) => {
        const pipeline = await client.apiRequest<any>({
          method: 'POST',
          url: '/crm/v3/pipelines/deals',
          data: {
            label: args.label,
            displayOrder: args.displayOrder || 0,
            stages: args.stages || [],
          },
        });

        return {
          content: [
            {
              type: 'text',
              text: JSON.stringify(
                {
                  success: true,
                  pipelineId: pipeline.id,
                  pipeline,
                },
                null,
                2
              ),
            },
          ],
        };
      },
    },

    {
      name: 'hubspot_update_deal_pipeline',
      description: 'Update an existing deal pipeline',
      inputSchema: {
        type: 'object',
        properties: {
          pipelineId: {
            type: 'string',
            description: 'Pipeline ID',
          },
          label: {
            type: 'string',
            description: 'Updated label',
          },
          displayOrder: {
            type: 'number',
            description: 'Updated display order',
          },
        },
        required: ['pipelineId'],
      },
      handler: async (args: any) => {
        const updates: any = {};
        if (args.label) updates.label = args.label;
        if (args.displayOrder !== undefined) updates.displayOrder = args.displayOrder;

        const pipeline = await client.apiRequest<any>({
          method: 'PATCH',
          url: `/crm/v3/pipelines/deals/${args.pipelineId}`,
          data: updates,
        });

        return {
          content: [
            {
              type: 'text',
              text: JSON.stringify(
                {
                  success: true,
                  pipeline,
                },
                null,
                2
              ),
            },
          ],
        };
      },
    },

    {
      name: 'hubspot_delete_deal_pipeline',
      description: 'Archive a deal pipeline',
      inputSchema: {
        type: 'object',
        properties: {
          pipelineId: {
            type: 'string',
            description: 'Pipeline ID to archive',
          },
        },
        required: ['pipelineId'],
      },
      handler: async (args: any) => {
        await client.apiRequest({
          method: 'DELETE',
          url: `/crm/v3/pipelines/deals/${args.pipelineId}`,
        });

        return {
          content: [
            {
              type: 'text',
              text: JSON.stringify(
                {
                  success: true,
                  message: `Pipeline ${args.pipelineId} archived successfully`,
                },
                null,
                2
              ),
            },
          ],
        };
      },
    },

    {
      name: 'hubspot_create_deal_stage',
      description: 'Create a new stage in a deal pipeline',
      inputSchema: {
        type: 'object',
        properties: {
          pipelineId: {
            type: 'string',
            description: 'Pipeline ID',
          },
          label: {
            type: 'string',
            description: 'Stage label/name',
          },
          displayOrder: {
            type: 'number',
            description: 'Display order',
          },
          metadata: {
            type: 'object',
            description: 'Stage metadata',
          },
        },
        required: ['pipelineId', 'label'],
      },
      handler: async (args: any) => {
        const stage = await client.apiRequest<any>({
          method: 'POST',
          url: `/crm/v3/pipelines/deals/${args.pipelineId}/stages`,
          data: {
            label: args.label,
            displayOrder: args.displayOrder || 0,
            metadata: args.metadata || {},
          },
        });

        return {
          content: [
            {
              type: 'text',
              text: JSON.stringify(
                {
                  success: true,
                  stageId: stage.id,
                  stage,
                },
                null,
                2
              ),
            },
          ],
        };
      },
    },

    // Ticket Pipelines
    {
      name: 'hubspot_list_ticket_pipelines',
      description: 'List all ticket pipelines',
      inputSchema: {
        type: 'object',
        properties: {},
      },
      handler: async () => {
        const pipelines = await client.getPipelines('tickets');

        return {
          content: [
            {
              type: 'text',
              text: JSON.stringify(
                {
                  total: pipelines.length,
                  pipelines,
                },
                null,
                2
              ),
            },
          ],
        };
      },
    },

    {
      name: 'hubspot_get_ticket_pipeline',
      description: 'Get a specific ticket pipeline by ID',
      inputSchema: {
        type: 'object',
        properties: {
          pipelineId: {
            type: 'string',
            description: 'Pipeline ID',
          },
        },
        required: ['pipelineId'],
      },
      handler: async (args: any) => {
        const pipeline = await client.apiRequest<any>({
          method: 'GET',
          url: `/crm/v3/pipelines/tickets/${args.pipelineId}`,
        });

        return {
          content: [
            {
              type: 'text',
              text: JSON.stringify(pipeline, null, 2),
            },
          ],
        };
      },
    },

    {
      name: 'hubspot_create_ticket_pipeline',
      description: 'Create a new ticket pipeline',
      inputSchema: {
        type: 'object',
        properties: {
          label: {
            type: 'string',
            description: 'Pipeline label/name',
          },
          displayOrder: {
            type: 'number',
            description: 'Display order',
          },
          stages: {
            type: 'array',
            items: {
              type: 'object',
              properties: {
                label: { type: 'string' },
                displayOrder: { type: 'number' },
                metadata: { type: 'object' },
              },
            },
            description: 'Array of pipeline stages',
          },
        },
        required: ['label'],
      },
      handler: async (args: any) => {
        const pipeline = await client.apiRequest<any>({
          method: 'POST',
          url: '/crm/v3/pipelines/tickets',
          data: {
            label: args.label,
            displayOrder: args.displayOrder || 0,
            stages: args.stages || [],
          },
        });

        return {
          content: [
            {
              type: 'text',
              text: JSON.stringify(
                {
                  success: true,
                  pipelineId: pipeline.id,
                  pipeline,
                },
                null,
                2
              ),
            },
          ],
        };
      },
    },

    {
      name: 'hubspot_update_ticket_pipeline',
      description: 'Update an existing ticket pipeline',
      inputSchema: {
        type: 'object',
        properties: {
          pipelineId: {
            type: 'string',
            description: 'Pipeline ID',
          },
          label: {
            type: 'string',
            description: 'Updated label',
          },
          displayOrder: {
            type: 'number',
            description: 'Updated display order',
          },
        },
        required: ['pipelineId'],
      },
      handler: async (args: any) => {
        const updates: any = {};
        if (args.label) updates.label = args.label;
        if (args.displayOrder !== undefined) updates.displayOrder = args.displayOrder;

        const pipeline = await client.apiRequest<any>({
          method: 'PATCH',
          url: `/crm/v3/pipelines/tickets/${args.pipelineId}`,
          data: updates,
        });

        return {
          content: [
            {
              type: 'text',
              text: JSON.stringify(
                {
                  success: true,
                  pipeline,
                },
                null,
                2
              ),
            },
          ],
        };
      },
    },

    {
      name: 'hubspot_delete_ticket_pipeline',
      description: 'Archive a ticket pipeline',
      inputSchema: {
        type: 'object',
        properties: {
          pipelineId: {
            type: 'string',
            description: 'Pipeline ID to archive',
          },
        },
        required: ['pipelineId'],
      },
      handler: async (args: any) => {
        await client.apiRequest({
          method: 'DELETE',
          url: `/crm/v3/pipelines/tickets/${args.pipelineId}`,
        });

        return {
          content: [
            {
              type: 'text',
              text: JSON.stringify(
                {
                  success: true,
                  message: `Pipeline ${args.pipelineId} archived successfully`,
                },
                null,
                2
              ),
            },
          ],
        };
      },
    },
  ];
}
