import type { WrikeClient } from '../clients/wrike.js';
import type { WrikeGroup } from '../types/index.js';

export function createGroupTools(client: WrikeClient) {
  return {
    // List groups
    wrike_list_groups: {
      name: 'wrike_list_groups',
      description: 'List all groups in the account',
      inputSchema: {
        type: 'object',
        properties: {
          metadata: { type: 'string', description: 'Metadata filter (JSON)' },
          fields: { type: 'array', items: { type: 'string' }, description: 'Additional fields' },
        },
      },
      handler: async (params: Record<string, unknown>) => {
        const queryParams: Record<string, unknown> = {};
        
        if (params.metadata) queryParams.metadata = params.metadata;
        if (params.fields) queryParams.fields = params.fields;
        
        const response = await client.get<WrikeGroup>('/groups', queryParams);
        
        return {
          content: [
            {
              type: 'text',
              text: JSON.stringify(response.data, null, 2),
            },
          ],
        };
      },
    },

    // Get group by ID
    wrike_get_group: {
      name: 'wrike_get_group',
      description: 'Get a specific group by ID',
      inputSchema: {
        type: 'object',
        properties: {
          groupId: { type: 'string', description: 'Group ID (required)' },
          fields: { type: 'array', items: { type: 'string' }, description: 'Additional fields' },
        },
        required: ['groupId'],
      },
      handler: async (params: { groupId: string; fields?: string[] }) => {
        const response = await client.get<WrikeGroup>(`/groups/${params.groupId}`, {
          fields: params.fields,
        });
        
        return {
          content: [
            {
              type: 'text',
              text: JSON.stringify(response.data[0], null, 2),
            },
          ],
        };
      },
    },

    // Create group
    wrike_create_group: {
      name: 'wrike_create_group',
      description: 'Create a new group',
      inputSchema: {
        type: 'object',
        properties: {
          title: { type: 'string', description: 'Group title (required)' },
          members: { type: 'array', items: { type: 'string' }, description: 'Member user IDs' },
          parent: { type: 'string', description: 'Parent group ID' },
          metadata: { type: 'array', items: { type: 'object' }, description: 'Metadata key-value pairs' },
        },
        required: ['title'],
      },
      handler: async (params: { title: string; [key: string]: unknown }) => {
        const body: Record<string, unknown> = {
          title: params.title,
        };
        
        if (params.members) body.members = params.members;
        if (params.parent) body.parent = params.parent;
        if (params.metadata) body.metadata = params.metadata;
        
        const response = await client.post<WrikeGroup>('/groups', body);
        
        return {
          content: [
            {
              type: 'text',
              text: JSON.stringify(response.data[0], null, 2),
            },
          ],
        };
      },
    },

    // Update group
    wrike_update_group: {
      name: 'wrike_update_group',
      description: 'Update an existing group',
      inputSchema: {
        type: 'object',
        properties: {
          groupId: { type: 'string', description: 'Group ID (required)' },
          title: { type: 'string', description: 'New group title' },
          members: { type: 'array', items: { type: 'string' }, description: 'New member list' },
          parent: { type: 'string', description: 'New parent group ID' },
          metadata: { type: 'array', items: { type: 'object' }, description: 'Metadata key-value pairs' },
        },
        required: ['groupId'],
      },
      handler: async (params: { groupId: string; [key: string]: unknown }) => {
        const body: Record<string, unknown> = {};
        
        if (params.title) body.title = params.title;
        if (params.members) body.members = params.members;
        if (params.parent) body.parent = params.parent;
        if (params.metadata) body.metadata = params.metadata;
        
        const response = await client.put<WrikeGroup>(`/groups/${params.groupId}`, body);
        
        return {
          content: [
            {
              type: 'text',
              text: JSON.stringify(response.data[0], null, 2),
            },
          ],
        };
      },
    },

    // Delete group
    wrike_delete_group: {
      name: 'wrike_delete_group',
      description: 'Delete a group',
      inputSchema: {
        type: 'object',
        properties: {
          groupId: { type: 'string', description: 'Group ID (required)' },
          test: { type: 'boolean', description: 'Test mode (check dependencies)' },
        },
        required: ['groupId'],
      },
      handler: async (params: { groupId: string; test?: boolean }) => {
        const response = await client.delete<WrikeGroup>(`/groups/${params.groupId}`, {
          test: params.test,
        });
        
        return {
          content: [
            {
              type: 'text',
              text: JSON.stringify(response.data[0], null, 2),
            },
          ],
        };
      },
    },
  };
}
