import { z } from 'zod';
import type { RipplingClient } from '../clients/rippling.js';

export function createCustomObjectsTools(client: RipplingClient) {
  return {
    rippling_list_custom_objects: {
      description: 'List custom objects of a specific type',
      inputSchema: z.object({
        objectType: z.string().describe('Custom object type name'),
        cursor: z.string().optional().describe('Pagination cursor'),
        limit: z.number().min(1).max(500).default(100).optional().describe('Number of results per page'),
      }),
      handler: async (args: any) => {
        const { objectType, ...params } = args;
        const result = await client.listCustomObjects(objectType, params);
        return {
          content: [
            {
              type: 'text',
              text: JSON.stringify(result, null, 2),
            },
          ],
        };
      },
    },

    rippling_get_custom_object: {
      description: 'Get a specific custom object',
      inputSchema: z.object({
        objectType: z.string().describe('Custom object type name'),
        id: z.string().describe('Object ID'),
      }),
      handler: async (args: any) => {
        const { objectType, id } = args;
        const result = await client.getCustomObject(objectType, id);
        return {
          content: [
            {
              type: 'text',
              text: JSON.stringify(result, null, 2),
            },
          ],
        };
      },
    },

    rippling_create_custom_object: {
      description: 'Create a new custom object',
      inputSchema: z.object({
        objectType: z.string().describe('Custom object type name'),
        fields: z.record(z.any()).describe('Object field values as key-value pairs'),
      }),
      handler: async (args: any) => {
        const { objectType, fields } = args;
        const result = await client.createCustomObject(objectType, fields);
        return {
          content: [
            {
              type: 'text',
              text: `Custom object created successfully:\n${JSON.stringify(result, null, 2)}`,
            },
          ],
        };
      },
    },

    rippling_update_custom_object: {
      description: 'Update a custom object',
      inputSchema: z.object({
        objectType: z.string().describe('Custom object type name'),
        id: z.string().describe('Object ID'),
        fields: z.record(z.any()).describe('Updated field values'),
      }),
      handler: async (args: any) => {
        const { objectType, id, fields } = args;
        const result = await client.updateCustomObject(objectType, id, fields);
        return {
          content: [
            {
              type: 'text',
              text: `Custom object updated successfully:\n${JSON.stringify(result, null, 2)}`,
            },
          ],
        };
      },
    },

    rippling_query_custom_objects: {
      description: 'Query custom objects with filters',
      inputSchema: z.object({
        objectType: z.string().describe('Custom object type name'),
        query: z.record(z.any()).describe('Query filters and conditions'),
      }),
      handler: async (args: any) => {
        const { objectType, query } = args;
        const result = await client.queryCustomObjects(objectType, query);
        return {
          content: [
            {
              type: 'text',
              text: JSON.stringify(result, null, 2),
            },
          ],
        };
      },
    },
  };
}
