/**
 * Bulk API 2.0 tools for large-scale data operations
 */

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

export function getTools(client: SalesforceClient) {
  return [
    {
      name: 'sf_bulk_create_job',
      description: 'Create a bulk job for insert, update, upsert, delete, or hardDelete operations',
      inputSchema: {
        type: 'object',
        properties: {
          objectName: {
            type: 'string',
            description: 'Object to perform bulk operation on (e.g., "Account", "Contact")',
          },
          operation: {
            type: 'string',
            description: 'Operation type: insert, update, upsert, delete, or hardDelete',
            enum: ['insert', 'update', 'upsert', 'delete', 'hardDelete'],
          },
          externalIdField: {
            type: 'string',
            description: 'External ID field name (required for upsert operations)',
          },
        },
        required: ['objectName', 'operation'],
      },
      handler: async (args: {
        objectName: string;
        operation: 'insert' | 'update' | 'upsert' | 'delete' | 'hardDelete';
        externalIdField?: string;
      }) => {
        const jobInfo = await client.createBulkJob({
          object: args.objectName,
          operation: args.operation,
          externalIdFieldName: args.externalIdField,
          contentType: 'CSV',
          lineEnding: 'LF',
        });

        return {
          jobId: jobInfo.id,
          object: jobInfo.object,
          operation: jobInfo.operation,
          state: jobInfo.state,
          createdDate: jobInfo.createdDate,
        };
      },
    },

    {
      name: 'sf_bulk_upload_data',
      description: 'Upload CSV data to a bulk job',
      inputSchema: {
        type: 'object',
        properties: {
          jobId: {
            type: 'string',
            description: 'The bulk job ID',
          },
          csvData: {
            type: 'string',
            description: 'CSV data with headers (e.g., "Name,Email\\nJohn,john@example.com\\nJane,jane@example.com")',
          },
        },
        required: ['jobId', 'csvData'],
      },
      handler: async (args: { jobId: string; csvData: string }) => {
        await client.uploadBulkData(args.jobId, args.csvData);
        return {
          success: true,
          jobId: args.jobId,
          message: 'Data uploaded successfully. Use sf_bulk_close_job to start processing.',
        };
      },
    },

    {
      name: 'sf_bulk_close_job',
      description: 'Close a bulk job and start processing the uploaded data',
      inputSchema: {
        type: 'object',
        properties: {
          jobId: {
            type: 'string',
            description: 'The bulk job ID to close',
          },
        },
        required: ['jobId'],
      },
      handler: async (args: { jobId: string }) => {
        const jobInfo = await client.closeBulkJob(args.jobId);
        return {
          jobId: jobInfo.id,
          state: jobInfo.state,
          message: 'Job closed and processing started. Use sf_bulk_get_job_status to check progress.',
        };
      },
    },

    {
      name: 'sf_bulk_get_job_status',
      description: 'Get the status of a bulk job',
      inputSchema: {
        type: 'object',
        properties: {
          jobId: {
            type: 'string',
            description: 'The bulk job ID',
          },
        },
        required: ['jobId'],
      },
      handler: async (args: { jobId: string }) => {
        const jobInfo = await client.getBulkJobInfo(args.jobId);
        return {
          jobId: jobInfo.id,
          object: jobInfo.object,
          operation: jobInfo.operation,
          state: jobInfo.state,
          recordsProcessed: jobInfo.numberRecordsProcessed,
          recordsFailed: jobInfo.numberRecordsFailed,
          createdDate: jobInfo.createdDate,
          systemModstamp: jobInfo.systemModstamp,
          totalProcessingTime: jobInfo.totalProcessingTime,
        };
      },
    },

    {
      name: 'sf_bulk_get_successful_results',
      description: 'Get successful results from a completed bulk job (returns CSV)',
      inputSchema: {
        type: 'object',
        properties: {
          jobId: {
            type: 'string',
            description: 'The bulk job ID',
          },
        },
        required: ['jobId'],
      },
      handler: async (args: { jobId: string }) => {
        const csvResults = await client.getBulkJobSuccessfulResults(args.jobId);
        return {
          jobId: args.jobId,
          format: 'CSV',
          data: csvResults,
        };
      },
    },

    {
      name: 'sf_bulk_get_failed_results',
      description: 'Get failed results from a completed bulk job with error details (returns CSV)',
      inputSchema: {
        type: 'object',
        properties: {
          jobId: {
            type: 'string',
            description: 'The bulk job ID',
          },
        },
        required: ['jobId'],
      },
      handler: async (args: { jobId: string }) => {
        const csvResults = await client.getBulkJobFailedResults(args.jobId);
        return {
          jobId: args.jobId,
          format: 'CSV',
          data: csvResults,
        };
      },
    },

    {
      name: 'sf_bulk_abort_job',
      description: 'Abort a bulk job that is in progress',
      inputSchema: {
        type: 'object',
        properties: {
          jobId: {
            type: 'string',
            description: 'The bulk job ID to abort',
          },
        },
        required: ['jobId'],
      },
      handler: async (args: { jobId: string }) => {
        const jobInfo = await client.abortBulkJob(args.jobId);
        return {
          jobId: jobInfo.id,
          state: jobInfo.state,
          message: 'Job aborted successfully',
        };
      },
    },
  ];
}
