/**
 * Basecamp Documents Tools (Docs & Files Vault)
 */

import { BasecampClient } from '../clients/basecamp.js';
import type { Document, CreateDocumentRequest, UpdateDocumentRequest } from '../types/index.js';

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

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

    {
      name: 'basecamp_document_create',
      description: 'Create a new document in a vault',
      inputSchema: {
        type: 'object',
        properties: {
          project_id: {
            type: 'number',
            description: 'The ID of the project',
          },
          vault_id: {
            type: 'number',
            description: 'The ID of the vault',
          },
          title: {
            type: 'string',
            description: 'Document title',
          },
          content: {
            type: 'string',
            description: 'Document content (HTML supported)',
          },
        },
        required: ['project_id', 'vault_id', 'title', 'content'],
      },
      handler: async (args: { project_id: number; vault_id: number } & CreateDocumentRequest) => {
        const { project_id, vault_id, ...payload } = args;
        const data = await client.post<Document>(
          `/buckets/${project_id}/vaults/${vault_id}/documents.json`,
          payload
        );
        return {
          content: [
            {
              type: 'text',
              text: JSON.stringify(data, null, 2),
            },
          ],
        };
      },
    },

    {
      name: 'basecamp_document_update',
      description: 'Update a document',
      inputSchema: {
        type: 'object',
        properties: {
          project_id: {
            type: 'number',
            description: 'The ID of the project',
          },
          document_id: {
            type: 'number',
            description: 'The ID of the document',
          },
          title: {
            type: 'string',
            description: 'Updated title',
          },
          content: {
            type: 'string',
            description: 'Updated content',
          },
        },
        required: ['project_id', 'document_id'],
      },
      handler: async (args: { project_id: number; document_id: number } & UpdateDocumentRequest) => {
        const { project_id, document_id, ...updates } = args;
        const data = await client.put<Document>(
          `/buckets/${project_id}/documents/${document_id}.json`,
          updates
        );
        return {
          content: [
            {
              type: 'text',
              text: JSON.stringify(data, null, 2),
            },
          ],
        };
      },
    },
  ];
}
