import { QuickBooksClient } from '../clients/quickbooks.js';
import type { TaxCode, TaxRate, TaxAgency } from '../types/index.js';

export function getTools(client: QuickBooksClient) {
  return [
    {
      name: 'qbo_list_tax_codes',
      description: 'List all tax codes',
      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)',
          },
          active: {
            type: 'boolean',
            description: 'Filter by active status',
          },
        },
      },
      handler: async (args: any) => {
        const { startPosition = 1, maxResults = 100, active } = args;
        
        let query = 'SELECT * FROM TaxCode';
        if (active !== undefined) {
          query += ` WHERE Active = ${active}`;
        }
        
        return await client.query<TaxCode>(query, { startPosition, maxResults });
      },
    },
    {
      name: 'qbo_get_tax_code',
      description: 'Get a specific tax code by ID',
      inputSchema: {
        type: 'object',
        properties: {
          id: {
            type: 'string',
            description: 'Tax code ID',
          },
        },
        required: ['id'],
      },
      handler: async (args: any) => {
        return await client.read<TaxCode>('TaxCode', args.id);
      },
    },
    {
      name: 'qbo_query_tax_codes',
      description: 'Run a custom SQL-like query on tax codes',
      inputSchema: {
        type: 'object',
        properties: {
          query: {
            type: 'string',
            description: 'SQL-like query (e.g., "SELECT * FROM TaxCode WHERE Active = true")',
          },
          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<TaxCode>(args.query, {
          startPosition: args.startPosition || 1,
          maxResults: args.maxResults || 100,
        });
      },
    },
    {
      name: 'qbo_list_tax_rates',
      description: 'List all tax rates',
      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)',
          },
          active: {
            type: 'boolean',
            description: 'Filter by active status',
          },
        },
      },
      handler: async (args: any) => {
        const { startPosition = 1, maxResults = 100, active } = args;
        
        let query = 'SELECT * FROM TaxRate';
        if (active !== undefined) {
          query += ` WHERE Active = ${active}`;
        }
        
        return await client.query<TaxRate>(query, { startPosition, maxResults });
      },
    },
    {
      name: 'qbo_get_tax_rate',
      description: 'Get a specific tax rate by ID',
      inputSchema: {
        type: 'object',
        properties: {
          id: {
            type: 'string',
            description: 'Tax rate ID',
          },
        },
        required: ['id'],
      },
      handler: async (args: any) => {
        return await client.read<TaxRate>('TaxRate', args.id);
      },
    },
    {
      name: 'qbo_query_tax_rates',
      description: 'Run a custom SQL-like query on tax rates',
      inputSchema: {
        type: 'object',
        properties: {
          query: {
            type: 'string',
            description: 'SQL-like query (e.g., "SELECT * FROM TaxRate WHERE RateValue > \'5.0\'")',
          },
          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<TaxRate>(args.query, {
          startPosition: args.startPosition || 1,
          maxResults: args.maxResults || 100,
        });
      },
    },
    {
      name: 'qbo_list_tax_agencies',
      description: 'List all tax agencies',
      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)',
          },
        },
      },
      handler: async (args: any) => {
        const { startPosition = 1, maxResults = 100 } = args;
        const query = 'SELECT * FROM TaxAgency';
        
        return await client.query<TaxAgency>(query, { startPosition, maxResults });
      },
    },
    {
      name: 'qbo_get_tax_agency',
      description: 'Get a specific tax agency by ID',
      inputSchema: {
        type: 'object',
        properties: {
          id: {
            type: 'string',
            description: 'Tax agency ID',
          },
        },
        required: ['id'],
      },
      handler: async (args: any) => {
        return await client.read<TaxAgency>('TaxAgency', args.id);
      },
    },
  ];
}
