import { z } from 'zod';
import { Tool } from '@modelcontextprotocol/sdk/types.js';
import type { NotionClient } from '../clients/notion.js';
import type { UserId } from '../types/index.js';

// Zod schemas
const GetUserSchema = z.object({
  user_id: z.string().describe('The ID of the user'),
});

const ListUsersSchema = z.object({
  page_size: z.number().min(1).max(100).optional().describe('Number of results to return'),
  start_cursor: z.string().optional().describe('Pagination cursor'),
});

const ListUsersAllSchema = z.object({});

const GetMeSchema = z.object({});

export function getTools(client: NotionClient): Tool[] {
  return [
    {
      name: 'notion_get_user',
      description:
        'Retrieve a user by ID. Returns user information including name, type (person/bot), and email for person users.',
      inputSchema: {
        type: 'object',
        properties: {
          user_id: { type: 'string', description: 'The ID of the user' },
        },
        required: ['user_id'],
      },
    },
    {
      name: 'notion_list_users',
      description:
        'List users in the workspace. Returns paginated list of all workspace members.',
      inputSchema: {
        type: 'object',
        properties: {
          page_size: {
            type: 'number',
            description: 'Number of results to return (max 100)',
          },
          start_cursor: {
            type: 'string',
            description: 'Pagination cursor',
          },
        },
      },
    },
    {
      name: 'notion_list_users_all',
      description:
        'List ALL users in the workspace (auto-paginating). Returns complete list of workspace members.',
      inputSchema: {
        type: 'object',
        properties: {},
      },
    },
    {
      name: 'notion_get_me',
      description:
        'Retrieve the bot user associated with the API token. Useful for identifying the current integration.',
      inputSchema: {
        type: 'object',
        properties: {},
      },
    },
  ];
}

export async function handleUserTool(
  toolName: string,
  args: any,
  client: NotionClient
): Promise<any> {
  switch (toolName) {
    case 'notion_get_user': {
      const validated = GetUserSchema.parse(args);
      const user = await client.getUser(validated.user_id as UserId);
      return user;
    }

    case 'notion_list_users': {
      const validated = ListUsersSchema.parse(args);
      const users = await client.listUsers(validated.start_cursor, validated.page_size);
      return users;
    }

    case 'notion_list_users_all': {
      ListUsersAllSchema.parse(args);
      const allUsers = [];
      for await (const user of client.listUsersAll()) {
        allUsers.push(user);
      }
      return { object: 'list', results: allUsers, has_more: false, next_cursor: null };
    }

    case 'notion_get_me': {
      GetMeSchema.parse(args);
      const botUser = await client.getBotUser();
      return botUser;
    }

    default:
      throw new Error(`Unknown user tool: ${toolName}`);
  }
}
