/**
 * Basecamp People Tools
 */

import { BasecampClient } from '../clients/basecamp.js';
import type { Person } from '../types/index.js';

export function registerPeopleTools(client: BasecampClient) {
  return [
    {
      name: 'basecamp_people_list',
      description: 'List all people in the Basecamp account',
      inputSchema: {
        type: 'object',
        properties: {},
      },
      handler: async () => {
        const { data } = await client.get<Person[]>('/people.json');
        return {
          content: [
            {
              type: 'text',
              text: JSON.stringify(data, null, 2),
            },
          ],
        };
      },
    },

    {
      name: 'basecamp_person_get',
      description: 'Get a specific person by ID',
      inputSchema: {
        type: 'object',
        properties: {
          person_id: {
            type: 'number',
            description: 'The ID of the person',
          },
        },
        required: ['person_id'],
      },
      handler: async (args: { person_id: number }) => {
        const { data } = await client.get<Person>(`/people/${args.person_id}.json`);
        return {
          content: [
            {
              type: 'text',
              text: JSON.stringify(data, null, 2),
            },
          ],
        };
      },
    },

    {
      name: 'basecamp_project_people_list',
      description: 'List all people with access to a project',
      inputSchema: {
        type: 'object',
        properties: {
          project_id: {
            type: 'number',
            description: 'The ID of the project',
          },
        },
        required: ['project_id'],
      },
      handler: async (args: { project_id: number }) => {
        const { data } = await client.get<Person[]>(`/projects/${args.project_id}/people.json`);
        return {
          content: [
            {
              type: 'text',
              text: JSON.stringify(data, null, 2),
            },
          ],
        };
      },
    },

    {
      name: 'basecamp_project_person_add',
      description: 'Grant a person access to a project',
      inputSchema: {
        type: 'object',
        properties: {
          project_id: {
            type: 'number',
            description: 'The ID of the project',
          },
          person_id: {
            type: 'number',
            description: 'The ID of the person to add',
          },
        },
        required: ['project_id', 'person_id'],
      },
      handler: async (args: { project_id: number; person_id: number }) => {
        await client.post(
          `/projects/${args.project_id}/people.json`,
          { person_id: args.person_id }
        );
        return {
          content: [
            {
              type: 'text',
              text: `Person ${args.person_id} added to project ${args.project_id}`,
            },
          ],
        };
      },
    },

    {
      name: 'basecamp_project_person_remove',
      description: 'Remove a person from a project',
      inputSchema: {
        type: 'object',
        properties: {
          project_id: {
            type: 'number',
            description: 'The ID of the project',
          },
          person_id: {
            type: 'number',
            description: 'The ID of the person to remove',
          },
        },
        required: ['project_id', 'person_id'],
      },
      handler: async (args: { project_id: number; person_id: number }) => {
        await client.delete(`/projects/${args.project_id}/people/${args.person_id}.json`);
        return {
          content: [
            {
              type: 'text',
              text: `Person ${args.person_id} removed from project ${args.project_id}`,
            },
          ],
        };
      },
    },

    {
      name: 'basecamp_profile_get',
      description: 'Get the current authenticated user profile',
      inputSchema: {
        type: 'object',
        properties: {},
      },
      handler: async () => {
        const { data } = await client.get<Person>('/my/profile.json');
        return {
          content: [
            {
              type: 'text',
              text: JSON.stringify(data, null, 2),
            },
          ],
        };
      },
    },
  ];
}
