import type { GustoClient } from '../clients/gusto.js';
import type { Garnishment } from '../types/index.js';

export function createGarnishmentTools(client: GustoClient) {
  return {
    // List garnishments for an employee
    list_garnishments: async (args: { employee_id: string }) => {
      const garnishments = await client.get<Garnishment[]>(
        `/employees/${args.employee_id}/garnishments`
      );
      return {
        content: [
          {
            type: 'text',
            text: JSON.stringify(garnishments, null, 2),
          },
        ],
      };
    },

    // Get a specific garnishment
    get_garnishment: async (args: { garnishment_id: string }) => {
      const garnishment = await client.get<Garnishment>(
        `/garnishments/${args.garnishment_id}`
      );
      return {
        content: [
          {
            type: 'text',
            text: JSON.stringify(garnishment, null, 2),
          },
        ],
      };
    },

    // Create a new garnishment
    create_garnishment: async (args: {
      employee_id: string;
      amount: string;
      description?: string;
      court_ordered?: boolean;
      times?: number;
      recurring?: boolean;
      annual_maximum?: string;
      pay_period_maximum?: string;
      deduct_as_percentage?: boolean;
    }) => {
      const garnishment = await client.post<Garnishment>(
        `/employees/${args.employee_id}/garnishments`,
        {
          active: true,
          amount: args.amount,
          description: args.description,
          court_ordered: args.court_ordered,
          times: args.times,
          recurring: args.recurring,
          annual_maximum: args.annual_maximum,
          pay_period_maximum: args.pay_period_maximum,
          deduct_as_percentage: args.deduct_as_percentage,
        }
      );
      return {
        content: [
          {
            type: 'text',
            text: `Garnishment created successfully:\n${JSON.stringify(garnishment, null, 2)}`,
          },
        ],
      };
    },

    // Update a garnishment
    update_garnishment: async (args: {
      garnishment_id: string;
      version: string;
      active?: boolean;
      amount?: string;
      description?: string;
      annual_maximum?: string;
      pay_period_maximum?: string;
    }) => {
      const garnishment = await client.put<Garnishment>(
        `/garnishments/${args.garnishment_id}`,
        {
          version: args.version,
          active: args.active,
          amount: args.amount,
          description: args.description,
          annual_maximum: args.annual_maximum,
          pay_period_maximum: args.pay_period_maximum,
        }
      );
      return {
        content: [
          {
            type: 'text',
            text: `Garnishment updated successfully:\n${JSON.stringify(garnishment, null, 2)}`,
          },
        ],
      };
    },
  };
}

export const garnishmentToolDefinitions = [
  {
    name: 'list_garnishments',
    description: 'List all garnishments for an employee',
    inputSchema: {
      type: 'object',
      properties: {
        employee_id: { type: 'string', description: 'Employee ID' },
      },
      required: ['employee_id'],
    },
  },
  {
    name: 'get_garnishment',
    description: 'Get detailed information about a specific garnishment',
    inputSchema: {
      type: 'object',
      properties: {
        garnishment_id: { type: 'string', description: 'Garnishment ID' },
      },
      required: ['garnishment_id'],
    },
  },
  {
    name: 'create_garnishment',
    description: 'Create a new garnishment for an employee',
    inputSchema: {
      type: 'object',
      properties: {
        employee_id: { type: 'string', description: 'Employee ID' },
        amount: { type: 'string', description: 'Garnishment amount' },
        description: { type: 'string', description: 'Description of garnishment' },
        court_ordered: { type: 'boolean', description: 'Whether court ordered' },
        times: { type: 'number', description: 'Number of times to deduct (for one-time)' },
        recurring: { type: 'boolean', description: 'Whether garnishment is recurring' },
        annual_maximum: { type: 'string', description: 'Annual maximum amount' },
        pay_period_maximum: { type: 'string', description: 'Pay period maximum amount' },
        deduct_as_percentage: {
          type: 'boolean',
          description: 'Whether amount is a percentage',
        },
      },
      required: ['employee_id', 'amount'],
    },
  },
  {
    name: 'update_garnishment',
    description: 'Update an existing garnishment',
    inputSchema: {
      type: 'object',
      properties: {
        garnishment_id: { type: 'string', description: 'Garnishment ID' },
        version: { type: 'string', description: 'Version for optimistic locking' },
        active: { type: 'boolean', description: 'Whether garnishment is active' },
        amount: { type: 'string', description: 'Garnishment amount' },
        description: { type: 'string', description: 'Description of garnishment' },
        annual_maximum: { type: 'string', description: 'Annual maximum amount' },
        pay_period_maximum: { type: 'string', description: 'Pay period maximum amount' },
      },
      required: ['garnishment_id', 'version'],
    },
  },
];
