/**
 * HubSpot Analytics Tools
 * Tools for accessing analytics data (traffic, sources, page views)
 */

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

export function getTools(client: HubSpotClient) {
  return [
    {
      name: 'hubspot_get_analytics_views',
      description: 'Get page view analytics for a specific time period',
      inputSchema: {
        type: 'object',
        properties: {
          startDate: {
            type: 'string',
            description: 'Start date (YYYY-MM-DD)',
          },
          endDate: {
            type: 'string',
            description: 'End date (YYYY-MM-DD)',
          },
          frequency: {
            type: 'string',
            description: 'Data frequency (DAILY, WEEKLY, MONTHLY)',
            default: 'DAILY',
          },
        },
        required: ['startDate', 'endDate'],
      },
      handler: async (args: any) => {
        const response = await client.apiRequest<any>({
          method: 'GET',
          url: '/analytics/v2/reports/totals',
          params: {
            start: args.startDate,
            end: args.endDate,
            frequency: args.frequency || 'DAILY',
          },
        });

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

    {
      name: 'hubspot_get_traffic_analytics',
      description: 'Get website traffic analytics',
      inputSchema: {
        type: 'object',
        properties: {
          startDate: {
            type: 'string',
            description: 'Start date (YYYY-MM-DD)',
          },
          endDate: {
            type: 'string',
            description: 'End date (YYYY-MM-DD)',
          },
          frequency: {
            type: 'string',
            description: 'Data frequency (DAILY, WEEKLY, MONTHLY)',
            default: 'DAILY',
          },
        },
        required: ['startDate', 'endDate'],
      },
      handler: async (args: any) => {
        const response = await client.apiRequest<any>({
          method: 'GET',
          url: '/analytics/v2/reports/traffic',
          params: {
            start: args.startDate,
            end: args.endDate,
            frequency: args.frequency || 'DAILY',
          },
        });

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

    {
      name: 'hubspot_get_sources_analytics',
      description: 'Get traffic sources analytics (where visitors come from)',
      inputSchema: {
        type: 'object',
        properties: {
          startDate: {
            type: 'string',
            description: 'Start date (YYYY-MM-DD)',
          },
          endDate: {
            type: 'string',
            description: 'End date (YYYY-MM-DD)',
          },
        },
        required: ['startDate', 'endDate'],
      },
      handler: async (args: any) => {
        const response = await client.apiRequest<any>({
          method: 'GET',
          url: '/analytics/v2/reports/sources',
          params: {
            start: args.startDate,
            end: args.endDate,
          },
        });

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

    {
      name: 'hubspot_get_page_analytics',
      description: 'Get analytics for specific pages',
      inputSchema: {
        type: 'object',
        properties: {
          startDate: {
            type: 'string',
            description: 'Start date (YYYY-MM-DD)',
          },
          endDate: {
            type: 'string',
            description: 'End date (YYYY-MM-DD)',
          },
          limit: {
            type: 'number',
            description: 'Number of top pages to return',
            default: 20,
          },
        },
        required: ['startDate', 'endDate'],
      },
      handler: async (args: any) => {
        const response = await client.apiRequest<any>({
          method: 'GET',
          url: '/analytics/v2/reports/pages',
          params: {
            start: args.startDate,
            end: args.endDate,
            limit: args.limit || 20,
          },
        });

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

    {
      name: 'hubspot_get_social_analytics',
      description: 'Get social media analytics',
      inputSchema: {
        type: 'object',
        properties: {
          startDate: {
            type: 'string',
            description: 'Start date (YYYY-MM-DD)',
          },
          endDate: {
            type: 'string',
            description: 'End date (YYYY-MM-DD)',
          },
        },
        required: ['startDate', 'endDate'],
      },
      handler: async (args: any) => {
        const response = await client.apiRequest<any>({
          method: 'GET',
          url: '/analytics/v2/reports/social',
          params: {
            start: args.startDate,
            end: args.endDate,
          },
        });

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

    {
      name: 'hubspot_get_conversion_analytics',
      description: 'Get conversion analytics (form submissions, landing pages)',
      inputSchema: {
        type: 'object',
        properties: {
          startDate: {
            type: 'string',
            description: 'Start date (YYYY-MM-DD)',
          },
          endDate: {
            type: 'string',
            description: 'End date (YYYY-MM-DD)',
          },
        },
        required: ['startDate', 'endDate'],
      },
      handler: async (args: any) => {
        const response = await client.apiRequest<any>({
          method: 'GET',
          url: '/analytics/v2/reports/conversions',
          params: {
            start: args.startDate,
            end: args.endDate,
          },
        });

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