/**
 * Basecamp Comments Tools
 */

import { BasecampClient } from '../clients/basecamp.js';
import type { Comment, CreateCommentRequest } from '../types/index.js';

export function registerCommentsTools(client: BasecampClient) {
  return [
    {
      name: 'basecamp_comments_list',
      description: 'List all comments on a recording (message, todo, document, etc.)',
      inputSchema: {
        type: 'object',
        properties: {
          project_id: {
            type: 'number',
            description: 'The ID of the project',
          },
          recording_id: {
            type: 'number',
            description: 'The ID of the recording (parent item)',
          },
        },
        required: ['project_id', 'recording_id'],
      },
      handler: async (args: { project_id: number; recording_id: number }) => {
        const { data } = await client.get<Comment[]>(
          `/buckets/${args.project_id}/recordings/${args.recording_id}/comments.json`
        );
        return {
          content: [
            {
              type: 'text',
              text: JSON.stringify(data, null, 2),
            },
          ],
        };
      },
    },

    {
      name: 'basecamp_comment_get',
      description: 'Get a specific comment by ID',
      inputSchema: {
        type: 'object',
        properties: {
          project_id: {
            type: 'number',
            description: 'The ID of the project',
          },
          comment_id: {
            type: 'number',
            description: 'The ID of the comment',
          },
        },
        required: ['project_id', 'comment_id'],
      },
      handler: async (args: { project_id: number; comment_id: number }) => {
        const { data } = await client.get<Comment>(
          `/buckets/${args.project_id}/comments/${args.comment_id}.json`
        );
        return {
          content: [
            {
              type: 'text',
              text: JSON.stringify(data, null, 2),
            },
          ],
        };
      },
    },

    {
      name: 'basecamp_comment_create',
      description: 'Create a comment on a recording',
      inputSchema: {
        type: 'object',
        properties: {
          project_id: {
            type: 'number',
            description: 'The ID of the project',
          },
          recording_id: {
            type: 'number',
            description: 'The ID of the recording to comment on',
          },
          content: {
            type: 'string',
            description: 'The comment content (HTML supported)',
          },
        },
        required: ['project_id', 'recording_id', 'content'],
      },
      handler: async (args: { project_id: number; recording_id: number } & CreateCommentRequest) => {
        const { project_id, recording_id, ...payload } = args;
        const data = await client.post<Comment>(
          `/buckets/${project_id}/recordings/${recording_id}/comments.json`,
          payload
        );
        return {
          content: [
            {
              type: 'text',
              text: JSON.stringify(data, null, 2),
            },
          ],
        };
      },
    },

    {
      name: 'basecamp_comment_update',
      description: 'Update a comment',
      inputSchema: {
        type: 'object',
        properties: {
          project_id: {
            type: 'number',
            description: 'The ID of the project',
          },
          comment_id: {
            type: 'number',
            description: 'The ID of the comment',
          },
          content: {
            type: 'string',
            description: 'Updated content',
          },
        },
        required: ['project_id', 'comment_id', 'content'],
      },
      handler: async (args: { project_id: number; comment_id: number; content: string }) => {
        const { project_id, comment_id, content } = args;
        const data = await client.put<Comment>(
          `/buckets/${project_id}/comments/${comment_id}.json`,
          { content }
        );
        return {
          content: [
            {
              type: 'text',
              text: JSON.stringify(data, null, 2),
            },
          ],
        };
      },
    },
  ];
}
