/**
 * HubSpot Lists Tools
 * Tools for managing contact lists (static and active/dynamic)
 */

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

export function getTools(client: HubSpotClient) {
  return [
    {
      name: 'hubspot_list_lists',
      description: 'List all contact lists',
      inputSchema: {
        type: 'object',
        properties: {
          limit: {
            type: 'number',
            description: 'Maximum number of lists 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: '/contacts/v1/lists',
          params: {
            count: args.limit || 20,
            offset: args.offset || 0,
          },
        });

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

    {
      name: 'hubspot_get_list',
      description: 'Get a specific contact list by ID',
      inputSchema: {
        type: 'object',
        properties: {
          listId: {
            type: 'string',
            description: 'List ID',
          },
        },
        required: ['listId'],
      },
      handler: async (args: any) => {
        const list = await client.apiRequest<any>({
          method: 'GET',
          url: `/contacts/v1/lists/${args.listId}`,
        });

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

    {
      name: 'hubspot_create_static_list',
      description: 'Create a new static contact list',
      inputSchema: {
        type: 'object',
        properties: {
          name: {
            type: 'string',
            description: 'List name',
          },
          description: {
            type: 'string',
            description: 'List description',
          },
        },
        required: ['name'],
      },
      handler: async (args: any) => {
        const list = await client.apiRequest<any>({
          method: 'POST',
          url: '/contacts/v1/lists',
          data: {
            name: args.name,
            dynamic: false,
            description: args.description,
          },
        });

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

    {
      name: 'hubspot_create_active_list',
      description: 'Create a new active/dynamic contact list with filters',
      inputSchema: {
        type: 'object',
        properties: {
          name: {
            type: 'string',
            description: 'List name',
          },
          description: {
            type: 'string',
            description: 'List description',
          },
          filters: {
            type: 'array',
            description: 'Array of filter criteria',
          },
        },
        required: ['name'],
      },
      handler: async (args: any) => {
        const list = await client.apiRequest<any>({
          method: 'POST',
          url: '/contacts/v1/lists',
          data: {
            name: args.name,
            dynamic: true,
            description: args.description,
            filters: args.filters || [],
          },
        });

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

    {
      name: 'hubspot_update_list',
      description: 'Update an existing contact list',
      inputSchema: {
        type: 'object',
        properties: {
          listId: {
            type: 'string',
            description: 'List ID',
          },
          name: {
            type: 'string',
            description: 'Updated list name',
          },
          description: {
            type: 'string',
            description: 'Updated description',
          },
        },
        required: ['listId'],
      },
      handler: async (args: any) => {
        const updates: any = {};
        if (args.name) updates.name = args.name;
        if (args.description) updates.description = args.description;

        const list = await client.apiRequest<any>({
          method: 'POST',
          url: `/contacts/v1/lists/${args.listId}`,
          data: updates,
        });

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

    {
      name: 'hubspot_delete_list',
      description: 'Delete a contact list',
      inputSchema: {
        type: 'object',
        properties: {
          listId: {
            type: 'string',
            description: 'List ID to delete',
          },
        },
        required: ['listId'],
      },
      handler: async (args: any) => {
        await client.apiRequest({
          method: 'DELETE',
          url: `/contacts/v1/lists/${args.listId}`,
        });

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

    {
      name: 'hubspot_add_contacts_to_list',
      description: 'Add contacts to a static list',
      inputSchema: {
        type: 'object',
        properties: {
          listId: {
            type: 'string',
            description: 'List ID',
          },
          contactIds: {
            type: 'array',
            items: { type: 'string' },
            description: 'Array of contact IDs to add',
          },
        },
        required: ['listId', 'contactIds'],
      },
      handler: async (args: any) => {
        const result = await client.apiRequest<any>({
          method: 'POST',
          url: `/contacts/v1/lists/${args.listId}/add`,
          data: {
            vids: args.contactIds.map((id: string) => parseInt(id, 10)),
          },
        });

        return {
          content: [
            {
              type: 'text',
              text: JSON.stringify(
                {
                  success: true,
                  message: `Added ${args.contactIds.length} contacts to list ${args.listId}`,
                  result,
                },
                null,
                2
              ),
            },
          ],
        };
      },
    },

    {
      name: 'hubspot_remove_contacts_from_list',
      description: 'Remove contacts from a static list',
      inputSchema: {
        type: 'object',
        properties: {
          listId: {
            type: 'string',
            description: 'List ID',
          },
          contactIds: {
            type: 'array',
            items: { type: 'string' },
            description: 'Array of contact IDs to remove',
          },
        },
        required: ['listId', 'contactIds'],
      },
      handler: async (args: any) => {
        const result = await client.apiRequest<any>({
          method: 'POST',
          url: `/contacts/v1/lists/${args.listId}/remove`,
          data: {
            vids: args.contactIds.map((id: string) => parseInt(id, 10)),
          },
        });

        return {
          content: [
            {
              type: 'text',
              text: JSON.stringify(
                {
                  success: true,
                  message: `Removed ${args.contactIds.length} contacts from list ${args.listId}`,
                  result,
                },
                null,
                2
              ),
            },
          ],
        };
      },
    },
  ];
}
