import { CloverClient } from '../clients/clover.js';
import { CloverTaxRate } from '../types/index.js';

export function createTaxesTools(client: CloverClient) {
  return {
    clover_list_tax_rates: {
      description: 'List all tax rates',
      inputSchema: {
        type: 'object',
        properties: {
          filter: {
            type: 'string',
            description: 'Filter expression',
          },
        },
      },
      handler: async (args: any) => {
        const taxRates = await client.fetchPaginated<CloverTaxRate>(
          '/tax_rates',
          { filter: args.filter }
        );
        return { taxRates, count: taxRates.length };
      },
    },

    clover_get_tax_rate: {
      description: 'Get a specific tax rate by ID',
      inputSchema: {
        type: 'object',
        properties: {
          taxRateId: { type: 'string', description: 'Tax rate ID' },
        },
        required: ['taxRateId'],
      },
      handler: async (args: any) => {
        return await client.get<CloverTaxRate>(`/tax_rates/${args.taxRateId}`);
      },
    },

    clover_create_tax_rate: {
      description: 'Create a new tax rate',
      inputSchema: {
        type: 'object',
        properties: {
          name: { type: 'string', description: 'Tax rate name' },
          rate: {
            type: 'number',
            description: 'Tax rate (e.g., 8.5 for 8.5%)',
          },
          isDefault: {
            type: 'boolean',
            description: 'Set as default tax rate',
          },
        },
        required: ['name', 'rate'],
      },
      handler: async (args: any) => {
        return await client.post<CloverTaxRate>('/tax_rates', {
          name: args.name,
          rate: args.rate,
          isDefault: args.isDefault,
        });
      },
    },

    clover_update_tax_rate: {
      description: 'Update an existing tax rate',
      inputSchema: {
        type: 'object',
        properties: {
          taxRateId: { type: 'string', description: 'Tax rate ID' },
          name: { type: 'string', description: 'Tax rate name' },
          rate: {
            type: 'number',
            description: 'Tax rate (e.g., 8.5 for 8.5%)',
          },
          isDefault: {
            type: 'boolean',
            description: 'Set as default tax rate',
          },
        },
        required: ['taxRateId'],
      },
      handler: async (args: any) => {
        const { taxRateId, ...updateData } = args;
        return await client.post<CloverTaxRate>(`/tax_rates/${taxRateId}`, updateData);
      },
    },

    clover_delete_tax_rate: {
      description: 'Delete a tax rate',
      inputSchema: {
        type: 'object',
        properties: {
          taxRateId: { type: 'string', description: 'Tax rate ID' },
        },
        required: ['taxRateId'],
      },
      handler: async (args: any) => {
        await client.delete(`/tax_rates/${args.taxRateId}`);
        return { success: true, taxRateId: args.taxRateId };
      },
    },
  };
}
