/**
 * HubSpot Workflows Tools
 * Tools for managing workflows (automation)
 */

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

export function getTools(client: HubSpotClient) {
  return [
    {
      name: 'hubspot_list_workflows',
      description: 'List all workflows',
      inputSchema: {
        type: 'object',
        properties: {
          limit: {
            type: 'number',
            description: 'Maximum number of workflows to return',
            default: 20,
          },
          offset: {
            type: 'number',
            description: 'Offset for pagination',
            default: 0,
          },
        },
      },
      handler: async (args: any) => {
        const response = await client.apiRequest<any>({
          method: 'GET',
          url: '/automation/v3/workflows',
          params: {
            limit: args.limit || 20,
            offset: args.offset || 0,
          },
        });

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

    {
      name: 'hubspot_get_workflow',
      description: 'Get a specific workflow by ID',
      inputSchema: {
        type: 'object',
        properties: {
          workflowId: {
            type: 'string',
            description: 'Workflow ID',
          },
        },
        required: ['workflowId'],
      },
      handler: async (args: any) => {
        const workflow = await client.apiRequest<any>({
          method: 'GET',
          url: `/automation/v3/workflows/${args.workflowId}`,
        });

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

    {
      name: 'hubspot_create_workflow',
      description: 'Create a new workflow',
      inputSchema: {
        type: 'object',
        properties: {
          name: {
            type: 'string',
            description: 'Workflow name',
          },
          type: {
            type: 'string',
            description: 'Workflow type (e.g., CONTACT_WORKFLOW)',
          },
          enabled: {
            type: 'boolean',
            description: 'Whether to activate immediately',
            default: false,
          },
        },
        required: ['name', 'type'],
      },
      handler: async (args: any) => {
        const workflow = await client.apiRequest<any>({
          method: 'POST',
          url: '/automation/v3/workflows',
          data: {
            name: args.name,
            type: args.type,
            enabled: args.enabled || false,
          },
        });

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

    {
      name: 'hubspot_activate_workflow',
      description: 'Activate a workflow',
      inputSchema: {
        type: 'object',
        properties: {
          workflowId: {
            type: 'string',
            description: 'Workflow ID to activate',
          },
        },
        required: ['workflowId'],
      },
      handler: async (args: any) => {
        const workflow = await client.apiRequest<any>({
          method: 'POST',
          url: `/automation/v3/workflows/${args.workflowId}/activate`,
        });

        return {
          content: [
            {
              type: 'text',
              text: JSON.stringify(
                {
                  success: true,
                  message: `Workflow ${args.workflowId} activated successfully`,
                  workflow,
                },
                null,
                2
              ),
            },
          ],
        };
      },
    },

    {
      name: 'hubspot_deactivate_workflow',
      description: 'Deactivate a workflow',
      inputSchema: {
        type: 'object',
        properties: {
          workflowId: {
            type: 'string',
            description: 'Workflow ID to deactivate',
          },
        },
        required: ['workflowId'],
      },
      handler: async (args: any) => {
        const workflow = await client.apiRequest<any>({
          method: 'POST',
          url: `/automation/v3/workflows/${args.workflowId}/deactivate`,
        });

        return {
          content: [
            {
              type: 'text',
              text: JSON.stringify(
                {
                  success: true,
                  message: `Workflow ${args.workflowId} deactivated successfully`,
                  workflow,
                },
                null,
                2
              ),
            },
          ],
        };
      },
    },

    {
      name: 'hubspot_enroll_contact_in_workflow',
      description: 'Manually enroll a contact in a workflow',
      inputSchema: {
        type: 'object',
        properties: {
          workflowId: {
            type: 'string',
            description: 'Workflow ID',
          },
          contactId: {
            type: 'string',
            description: 'Contact ID to enroll',
          },
        },
        required: ['workflowId', 'contactId'],
      },
      handler: async (args: any) => {
        const result = await client.apiRequest<any>({
          method: 'POST',
          url: `/automation/v2/workflows/${args.workflowId}/enrollments/contacts/${args.contactId}`,
        });

        return {
          content: [
            {
              type: 'text',
              text: JSON.stringify(
                {
                  success: true,
                  message: `Contact ${args.contactId} enrolled in workflow ${args.workflowId}`,
                  result,
                },
                null,
                2
              ),
            },
          ],
        };
      },
    },
  ];
}
