/**
 * Basecamp Messages Tools (Message Board Posts)
 */

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

export function registerMessagesTools(client: BasecampClient) {
  return [
    {
      name: 'basecamp_messages_list',
      description: 'List all messages in a project message board',
      inputSchema: {
        type: 'object',
        properties: {
          project_id: {
            type: 'number',
            description: 'The ID of the project',
          },
          message_board_id: {
            type: 'number',
            description: 'The ID of the message board',
          },
          status: {
            type: 'string',
            enum: ['active', 'archived', 'trashed'],
            description: 'Filter by message status',
          },
        },
        required: ['project_id', 'message_board_id'],
      },
      handler: async (args: { project_id: number; message_board_id: number; status?: string }) => {
        const { data } = await client.get<Message[]>(
          `/buckets/${args.project_id}/message_boards/${args.message_board_id}/messages.json`,
          args.status ? { status: args.status } : undefined
        );
        return {
          content: [
            {
              type: 'text',
              text: JSON.stringify(data, null, 2),
            },
          ],
        };
      },
    },

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

    {
      name: 'basecamp_message_create',
      description: 'Create a new message on the message board',
      inputSchema: {
        type: 'object',
        properties: {
          project_id: {
            type: 'number',
            description: 'The ID of the project',
          },
          message_board_id: {
            type: 'number',
            description: 'The ID of the message board',
          },
          subject: {
            type: 'string',
            description: 'The subject/title of the message',
          },
          content: {
            type: 'string',
            description: 'The content/body of the message (HTML supported)',
          },
          category_id: {
            type: 'number',
            description: 'Optional category ID',
          },
        },
        required: ['project_id', 'message_board_id', 'subject', 'content'],
      },
      handler: async (args: { project_id: number; message_board_id: number } & CreateMessageRequest) => {
        const { project_id, message_board_id, ...payload } = args;
        const data = await client.post<Message>(
          `/buckets/${project_id}/message_boards/${message_board_id}/messages.json`,
          payload
        );
        return {
          content: [
            {
              type: 'text',
              text: JSON.stringify(data, null, 2),
            },
          ],
        };
      },
    },

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

    {
      name: 'basecamp_message_trash',
      description: 'Move a message to trash',
      inputSchema: {
        type: 'object',
        properties: {
          project_id: {
            type: 'number',
            description: 'The ID of the project',
          },
          message_id: {
            type: 'number',
            description: 'The ID of the message',
          },
        },
        required: ['project_id', 'message_id'],
      },
      handler: async (args: { project_id: number; message_id: number }) => {
        await client.delete(`/buckets/${args.project_id}/messages/${args.message_id}.json`);
        return {
          content: [
            {
              type: 'text',
              text: `Message ${args.message_id} moved to trash`,
            },
          ],
        };
      },
    },
  ];
}
