/**
 * HubSpot Marketing Emails Tools
 * Tools for managing marketing emails in HubSpot
 */

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

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

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

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

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

    {
      name: 'hubspot_create_email',
      description: 'Create a new marketing email',
      inputSchema: {
        type: 'object',
        properties: {
          name: {
            type: 'string',
            description: 'Email name',
          },
          subject: {
            type: 'string',
            description: 'Email subject line',
          },
          fromName: {
            type: 'string',
            description: 'Sender name',
          },
          replyTo: {
            type: 'string',
            description: 'Reply-to email address',
          },
        },
        required: ['name', 'subject'],
      },
      handler: async (args: any) => {
        const email = await client.apiRequest<any>({
          method: 'POST',
          url: '/marketing/v3/emails',
          data: {
            name: args.name,
            subject: args.subject,
            fromName: args.fromName,
            replyTo: args.replyTo,
          },
        });

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

    {
      name: 'hubspot_update_email',
      description: 'Update an existing marketing email',
      inputSchema: {
        type: 'object',
        properties: {
          emailId: {
            type: 'string',
            description: 'Email ID to update',
          },
          name: {
            type: 'string',
            description: 'Updated email name',
          },
          subject: {
            type: 'string',
            description: 'Updated subject line',
          },
          fromName: {
            type: 'string',
            description: 'Updated sender name',
          },
        },
        required: ['emailId'],
      },
      handler: async (args: any) => {
        const updates: any = {};
        if (args.name) updates.name = args.name;
        if (args.subject) updates.subject = args.subject;
        if (args.fromName) updates.fromName = args.fromName;

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

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

    {
      name: 'hubspot_send_email',
      description: 'Send or schedule a marketing email',
      inputSchema: {
        type: 'object',
        properties: {
          emailId: {
            type: 'string',
            description: 'Email ID to send',
          },
        },
        required: ['emailId'],
      },
      handler: async (args: any) => {
        const result = await client.apiRequest<any>({
          method: 'POST',
          url: `/marketing/v3/emails/${args.emailId}/send`,
        });

        return {
          content: [
            {
              type: 'text',
              text: JSON.stringify(
                {
                  success: true,
                  message: 'Email sent successfully',
                  result,
                },
                null,
                2
              ),
            },
          ],
        };
      },
    },

    {
      name: 'hubspot_clone_email',
      description: 'Clone/duplicate an existing marketing email',
      inputSchema: {
        type: 'object',
        properties: {
          emailId: {
            type: 'string',
            description: 'Email ID to clone',
          },
          name: {
            type: 'string',
            description: 'Name for the cloned email',
          },
        },
        required: ['emailId'],
      },
      handler: async (args: any) => {
        const cloned = await client.apiRequest<any>({
          method: 'POST',
          url: `/marketing/v3/emails/${args.emailId}/clone`,
          data: args.name ? { name: args.name } : {},
        });

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

    {
      name: 'hubspot_get_email_stats',
      description: 'Get statistics for a marketing email (sends, opens, clicks)',
      inputSchema: {
        type: 'object',
        properties: {
          emailId: {
            type: 'string',
            description: 'Email ID',
          },
        },
        required: ['emailId'],
      },
      handler: async (args: any) => {
        const stats = await client.apiRequest<any>({
          method: 'GET',
          url: `/marketing/v3/emails/${args.emailId}/statistics`,
        });

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