/**
 * Dashboard management tools
 */

import type { SalesforceClient } from '../clients/salesforce.js';
import type { Dashboard } from '../types/index.js';

export function getTools(client: SalesforceClient) {
  return [
    {
      name: 'sf_list_dashboards',
      description: 'List available dashboards. Returns up to 200 dashboards by default.',
      inputSchema: {
        type: 'object',
        properties: {
          limit: {
            type: 'number',
            description: 'Maximum number of dashboards to return (default: 200)',
            default: 200,
          },
          offset: {
            type: 'number',
            description: 'Number of records to skip (for pagination)',
          },
        },
      },
      handler: async (args: { limit?: number; offset?: number }) => {
        const soql = client.buildSOQL({
          select: ['Id', 'Title', 'DeveloperName', 'FolderName', 'Description', 'RunningUser.Name', 'CreatedDate'],
          from: 'Dashboard',
          orderBy: 'Title ASC',
          limit: args.limit || 200,
          offset: args.offset,
        });

        const result = await client.query<Dashboard>(soql);
        return {
          totalSize: result.totalSize,
          records: result.records,
          hasMore: !result.done,
        };
      },
    },

    {
      name: 'sf_get_dashboard',
      description: 'Get detailed information about a specific dashboard by ID',
      inputSchema: {
        type: 'object',
        properties: {
          dashboardId: {
            type: 'string',
            description: 'The Salesforce ID of the dashboard (15 or 18 characters)',
          },
        },
        required: ['dashboardId'],
      },
      handler: async (args: { dashboardId: string }) => {
        const dashboard = await client.getRecord<Dashboard>('Dashboard', args.dashboardId);
        return dashboard;
      },
    },

    {
      name: 'sf_describe_dashboard',
      description: 'Get detailed metadata about a dashboard including components',
      inputSchema: {
        type: 'object',
        properties: {
          dashboardId: {
            type: 'string',
            description: 'The Salesforce ID of the dashboard',
          },
        },
        required: ['dashboardId'],
      },
      handler: async (args: { dashboardId: string }) => {
        // Use the Analytics API to describe the dashboard
        const response = await client['client'].get(`/analytics/dashboards/${args.dashboardId}/describe`);
        return response.data;
      },
    },

    {
      name: 'sf_get_dashboard_components',
      description: 'Get the components (charts, tables, metrics) of a dashboard',
      inputSchema: {
        type: 'object',
        properties: {
          dashboardId: {
            type: 'string',
            description: 'The Salesforce ID of the dashboard',
          },
        },
        required: ['dashboardId'],
      },
      handler: async (args: { dashboardId: string }) => {
        // Query DashboardComponent objects
        const soql = client.buildSOQL({
          select: ['Id', 'Name', 'DashboardId'],
          from: 'DashboardComponent',
          where: `DashboardId = '${args.dashboardId}'`,
          orderBy: 'Name ASC',
        });

        const result = await client.query(soql);
        return result.records;
      },
    },

    {
      name: 'sf_search_dashboards',
      description: 'Search for dashboards by title or developer name',
      inputSchema: {
        type: 'object',
        properties: {
          searchText: {
            type: 'string',
            description: 'Text to search in dashboard title or developer name',
          },
          folderName: {
            type: 'string',
            description: 'Filter by folder name',
          },
          limit: {
            type: 'number',
            description: 'Maximum number of results',
            default: 50,
          },
        },
      },
      handler: async (args: { searchText?: string; folderName?: string; limit?: number }) => {
        const conditions: string[] = [];
        
        if (args.searchText) {
          conditions.push(`(Title LIKE '%${args.searchText}%' OR DeveloperName LIKE '%${args.searchText}%')`);
        }
        
        if (args.folderName) {
          conditions.push(`FolderName = '${args.folderName}'`);
        }

        const soql = client.buildSOQL({
          select: ['Id', 'Title', 'DeveloperName', 'FolderName', 'Description', 'RunningUser.Name'],
          from: 'Dashboard',
          where: conditions.length > 0 ? conditions.join(' AND ') : undefined,
          orderBy: 'Title ASC',
          limit: args.limit || 50,
        });

        const result = await client.query<Dashboard>(soql);
        return result.records;
      },
    },
  ];
}
