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

export function createGroupsTools(client: RipplingClient) {
  return {
    rippling_list_groups: {
      description: 'List all groups',
      inputSchema: z.object({
        type: z.string().optional().describe('Filter by group type'),
        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 result = await client.listGroups(args);
        return {
          content: [
            {
              type: 'text',
              text: JSON.stringify(result, null, 2),
            },
          ],
        };
      },
    },

    rippling_get_group: {
      description: 'Get detailed group information including members',
      inputSchema: z.object({
        id: z.string().describe('Group ID'),
      }),
      handler: async (args: any) => {
        const result = await client.getGroup(args.id);
        return {
          content: [
            {
              type: 'text',
              text: JSON.stringify(result, null, 2),
            },
          ],
        };
      },
    },

    rippling_create_group: {
      description: 'Create a new group',
      inputSchema: z.object({
        name: z.string().describe('Group name'),
        description: z.string().optional().describe('Group description'),
        type: z.string().optional().describe('Group type'),
      }),
      handler: async (args: any) => {
        const result = await client.createGroup(args);
        return {
          content: [
            {
              type: 'text',
              text: `Group created successfully:\n${JSON.stringify(result, null, 2)}`,
            },
          ],
        };
      },
    },

    rippling_update_group: {
      description: 'Update group information',
      inputSchema: z.object({
        id: z.string().describe('Group ID'),
        name: z.string().optional().describe('Group name'),
        description: z.string().optional().describe('Group description'),
        type: z.string().optional().describe('Group type'),
      }),
      handler: async (args: any) => {
        const { id, ...data } = args;
        const result = await client.updateGroup(id, data);
        return {
          content: [
            {
              type: 'text',
              text: `Group updated successfully:\n${JSON.stringify(result, null, 2)}`,
            },
          ],
        };
      },
    },

    rippling_add_group_member: {
      description: 'Add a member to a group',
      inputSchema: z.object({
        groupId: z.string().describe('Group ID'),
        memberId: z.string().describe('Member (employee) ID to add'),
      }),
      handler: async (args: any) => {
        const { groupId, memberId } = args;
        const result = await client.addGroupMember(groupId, memberId);
        return {
          content: [
            {
              type: 'text',
              text: `Member added to group successfully:\n${JSON.stringify(result, null, 2)}`,
            },
          ],
        };
      },
    },

    rippling_remove_group_member: {
      description: 'Remove a member from a group',
      inputSchema: z.object({
        groupId: z.string().describe('Group ID'),
        memberId: z.string().describe('Member (employee) ID to remove'),
      }),
      handler: async (args: any) => {
        const { groupId, memberId } = args;
        const result = await client.removeGroupMember(groupId, memberId);
        return {
          content: [
            {
              type: 'text',
              text: `Member removed from group successfully:\n${JSON.stringify(result, null, 2)}`,
            },
          ],
        };
      },
    },
  };
}
