import { QuickBooksClient } from '../clients/quickbooks.js';
import type { Account } from '../types/index.js';

export function getTools(client: QuickBooksClient) {
  return [
    {
      name: 'qbo_list_accounts',
      description: 'List all accounts (chart of accounts) with optional filters',
      inputSchema: {
        type: 'object',
        properties: {
          startPosition: {
            type: 'number',
            description: 'Starting position (1-indexed, default 1)',
          },
          maxResults: {
            type: 'number',
            description: 'Maximum results to return (max 1000, default 100)',
          },
          accountType: {
            type: 'string',
            description: 'Filter by account type (e.g., Bank, Expense, Income, Asset, Liability, Equity)',
          },
          active: {
            type: 'boolean',
            description: 'Filter by active status',
          },
        },
      },
      handler: async (args: any) => {
        const { startPosition = 1, maxResults = 100, accountType, active } = args;
        
        let query = 'SELECT * FROM Account';
        const conditions: string[] = [];
        
        if (accountType) conditions.push(`AccountType = '${accountType}'`);
        if (active !== undefined) conditions.push(`Active = ${active}`);
        
        if (conditions.length > 0) {
          query += ' WHERE ' + conditions.join(' AND ');
        }
        
        return await client.query<Account>(query, { startPosition, maxResults });
      },
    },
    {
      name: 'qbo_get_account',
      description: 'Get a specific account by ID',
      inputSchema: {
        type: 'object',
        properties: {
          id: {
            type: 'string',
            description: 'Account ID',
          },
        },
        required: ['id'],
      },
      handler: async (args: any) => {
        return await client.read<Account>('Account', args.id);
      },
    },
    {
      name: 'qbo_create_account',
      description: 'Create a new account',
      inputSchema: {
        type: 'object',
        properties: {
          name: {
            type: 'string',
            description: 'Account name',
          },
          accountType: {
            type: 'string',
            description: 'Account type (e.g., Bank, Expense, Income, Asset, Liability, Equity, CreditCard)',
          },
          accountSubType: {
            type: 'string',
            description: 'Account sub-type (e.g., CashOnHand, Checking, Savings, etc.)',
          },
          description: {
            type: 'string',
            description: 'Account description',
          },
          currentBalance: {
            type: 'number',
            description: 'Current balance (for some account types)',
          },
          parentAccountId: {
            type: 'string',
            description: 'Parent account ID (for sub-accounts)',
          },
        },
        required: ['name', 'accountType'],
      },
      handler: async (args: any) => {
        const account: any = {
          Name: args.name,
          AccountType: args.accountType,
        };
        
        if (args.accountSubType) account.AccountSubType = args.accountSubType;
        if (args.description) account.Description = args.description;
        if (args.currentBalance !== undefined) account.CurrentBalance = args.currentBalance;
        if (args.parentAccountId) account.ParentRef = { value: args.parentAccountId };
        
        return await client.create<Account>('Account', account);
      },
    },
    {
      name: 'qbo_update_account',
      description: 'Update an existing account (requires SyncToken)',
      inputSchema: {
        type: 'object',
        properties: {
          id: {
            type: 'string',
            description: 'Account ID',
          },
          syncToken: {
            type: 'string',
            description: 'SyncToken from the account',
          },
          name: {
            type: 'string',
            description: 'Account name',
          },
          description: {
            type: 'string',
            description: 'Account description',
          },
          active: {
            type: 'boolean',
            description: 'Active status',
          },
        },
        required: ['id', 'syncToken'],
      },
      handler: async (args: any) => {
        const account: any = {
          Id: args.id,
          SyncToken: args.syncToken,
        };
        
        if (args.name) account.Name = args.name;
        if (args.description) account.Description = args.description;
        if (args.active !== undefined) account.Active = args.active;
        
        return await client.update<Account>('Account', account);
      },
    },
    {
      name: 'qbo_query_accounts',
      description: 'Run a custom SQL-like query on accounts',
      inputSchema: {
        type: 'object',
        properties: {
          query: {
            type: 'string',
            description: 'SQL-like query (e.g., "SELECT * FROM Account WHERE AccountType = \'Bank\'")',
          },
          startPosition: {
            type: 'number',
            description: 'Starting position (default 1)',
          },
          maxResults: {
            type: 'number',
            description: 'Maximum results (default 100)',
          },
        },
        required: ['query'],
      },
      handler: async (args: any) => {
        return await client.query<Account>(args.query, {
          startPosition: args.startPosition || 1,
          maxResults: args.maxResults || 100,
        });
      },
    },
  ];
}
