/**
 * HubSpot Forms Tools
 * Tools for managing forms and form submissions
 */

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

export function getTools(client: HubSpotClient) {
  return [
    {
      name: 'hubspot_list_forms',
      description: 'List all forms',
      inputSchema: {
        type: 'object',
        properties: {
          limit: {
            type: 'number',
            description: 'Maximum number of forms 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: '/marketing/v3/forms',
          params: {
            limit: args.limit || 20,
            after: args.offset || 0,
          },
        });

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

    {
      name: 'hubspot_get_form',
      description: 'Get a specific form by ID',
      inputSchema: {
        type: 'object',
        properties: {
          formId: {
            type: 'string',
            description: 'Form ID',
          },
        },
        required: ['formId'],
      },
      handler: async (args: any) => {
        const form = await client.apiRequest<any>({
          method: 'GET',
          url: `/marketing/v3/forms/${args.formId}`,
        });

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

    {
      name: 'hubspot_create_form',
      description: 'Create a new form',
      inputSchema: {
        type: 'object',
        properties: {
          name: {
            type: 'string',
            description: 'Form name',
          },
          formType: {
            type: 'string',
            description: 'Form type (e.g., "hubspot")',
          },
          submitText: {
            type: 'string',
            description: 'Submit button text',
          },
          redirect: {
            type: 'string',
            description: 'Redirect URL after submission',
          },
        },
        required: ['name'],
      },
      handler: async (args: any) => {
        const form = await client.apiRequest<any>({
          method: 'POST',
          url: '/marketing/v3/forms',
          data: {
            name: args.name,
            formType: args.formType || 'hubspot',
            submitText: args.submitText || 'Submit',
            redirect: args.redirect,
          },
        });

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

    {
      name: 'hubspot_update_form',
      description: 'Update an existing form',
      inputSchema: {
        type: 'object',
        properties: {
          formId: {
            type: 'string',
            description: 'Form ID',
          },
          name: {
            type: 'string',
            description: 'Updated form name',
          },
          submitText: {
            type: 'string',
            description: 'Updated submit button text',
          },
        },
        required: ['formId'],
      },
      handler: async (args: any) => {
        const updates: any = {};
        if (args.name) updates.name = args.name;
        if (args.submitText) updates.submitText = args.submitText;

        const form = await client.apiRequest<any>({
          method: 'PATCH',
          url: `/marketing/v3/forms/${args.formId}`,
          data: updates,
        });

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

    {
      name: 'hubspot_delete_form',
      description: 'Archive a form',
      inputSchema: {
        type: 'object',
        properties: {
          formId: {
            type: 'string',
            description: 'Form ID to archive',
          },
        },
        required: ['formId'],
      },
      handler: async (args: any) => {
        await client.apiRequest({
          method: 'DELETE',
          url: `/marketing/v3/forms/${args.formId}`,
        });

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

    {
      name: 'hubspot_get_form_submissions',
      description: 'Get submissions for a specific form',
      inputSchema: {
        type: 'object',
        properties: {
          formId: {
            type: 'string',
            description: 'Form ID',
          },
          limit: {
            type: 'number',
            description: 'Maximum number of submissions to return',
            default: 20,
          },
          after: {
            type: 'string',
            description: 'Pagination cursor',
          },
        },
        required: ['formId'],
      },
      handler: async (args: any) => {
        const response = await client.apiRequest<any>({
          method: 'GET',
          url: `/form-integrations/v1/submissions/forms/${args.formId}`,
          params: {
            limit: args.limit || 20,
            after: args.after,
          },
        });

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