/**
 * Xero MCP Tools Index
 * Aggregates all tool modules and provides unified access
 */

import { XeroClient } from '../clients/xero.js';
import type { Tool } from '@modelcontextprotocol/sdk/types.js';

import { getTools as getInvoiceTools, handleInvoiceTool } from './invoices.js';
import { getTools as getContactTools, handleContactTool } from './contacts.js';
import { getTools as getAccountTools, handleAccountTool } from './accounts.js';
import { getTools as getBankTransactionTools, handleBankTransactionTool } from './bank-transactions.js';
import { getTools as getPaymentTools, handlePaymentTool } from './payments.js';
import { getTools as getBillTools, handleBillTool } from './bills.js';
import { getTools as getCreditNoteTools, handleCreditNoteTool } from './credit-notes.js';
import { getTools as getPurchaseOrderTools, handlePurchaseOrderTool } from './purchase-orders.js';
import { getTools as getQuoteTools, handleQuoteTool } from './quotes.js';
import { getTools as getReportTools, handleReportTool } from './reports.js';
import { getTools as getEmployeeTools, handleEmployeeTool } from './employees.js';
import { getTools as getPayrollTools, handlePayrollTool } from './payroll.js';
import { getTools as getTaxRateTools, handleTaxRateTool } from './tax-rates.js';

/**
 * Get all Xero tools
 */
export function getAllTools(client: XeroClient): Tool[] {
  return [
    ...getInvoiceTools(client),
    ...getContactTools(client),
    ...getAccountTools(client),
    ...getBankTransactionTools(client),
    ...getPaymentTools(client),
    ...getBillTools(client),
    ...getCreditNoteTools(client),
    ...getPurchaseOrderTools(client),
    ...getQuoteTools(client),
    ...getReportTools(client),
    ...getEmployeeTools(client),
    ...getPayrollTools(client),
    ...getTaxRateTools(client)
  ];
}

/**
 * Handle tool execution by delegating to the appropriate handler
 */
export async function handleToolCall(
  toolName: string,
  args: Record<string, unknown>,
  client: XeroClient
): Promise<unknown> {
  // Invoice tools
  if (toolName.startsWith('xero_') && (
    toolName.includes('invoice') || 
    toolName.includes('attachment')
  ) && !toolName.includes('bill') && !toolName.includes('quote')) {
    return handleInvoiceTool(toolName, args, client);
  }

  // Contact tools
  if (toolName.includes('contact')) {
    return handleContactTool(toolName, args, client);
  }

  // Account tools
  if (toolName.includes('account') && !toolName.includes('bank')) {
    return handleAccountTool(toolName, args, client);
  }

  // Bank transaction tools
  if (toolName.includes('bank_transaction')) {
    return handleBankTransactionTool(toolName, args, client);
  }

  // Payment tools (includes prepayment and overpayment)
  if (toolName.includes('payment') || toolName.includes('prepayment') || toolName.includes('overpayment')) {
    return handlePaymentTool(toolName, args, client);
  }

  // Bill tools
  if (toolName.includes('bill')) {
    return handleBillTool(toolName, args, client);
  }

  // Credit note tools
  if (toolName.includes('credit_note')) {
    return handleCreditNoteTool(toolName, args, client);
  }

  // Purchase order tools
  if (toolName.includes('purchase_order')) {
    return handlePurchaseOrderTool(toolName, args, client);
  }

  // Quote tools
  if (toolName.includes('quote')) {
    return handleQuoteTool(toolName, args, client);
  }

  // Report tools
  if (toolName.includes('get_profit') || 
      toolName.includes('get_balance') || 
      toolName.includes('get_trial') ||
      toolName.includes('get_bank_summary') ||
      toolName.includes('get_aged') ||
      toolName.includes('get_executive') ||
      toolName.includes('get_budget')) {
    return handleReportTool(toolName, args, client);
  }

  // Employee tools
  if (toolName.includes('employee')) {
    return handleEmployeeTool(toolName, args, client);
  }

  // Payroll tools
  if (toolName.includes('pay_run') || 
      toolName.includes('pay_slip') || 
      toolName.includes('leave') ||
      toolName.includes('timesheet')) {
    return handlePayrollTool(toolName, args, client);
  }

  // Tax rate tools
  if (toolName.includes('tax_rate')) {
    return handleTaxRateTool(toolName, args, client);
  }

  throw new Error(`Unknown tool: ${toolName}`);
}

/**
 * Get tool count
 */
export function getToolCount(client: XeroClient): number {
  return getAllTools(client).length;
}
