import type { WrikeClient } from '../clients/wrike.js';
import type { WrikeAttachment } from '../types/index.js';

export function createAttachmentTools(client: WrikeClient) {
  return {
    // List attachments
    wrike_list_attachments: {
      name: 'wrike_list_attachments',
      description: 'List attachments on a task, folder, or comment',
      inputSchema: {
        type: 'object',
        properties: {
          taskId: { type: 'string', description: 'Task ID to get attachments from' },
          folderId: { type: 'string', description: 'Folder ID to get attachments from' },
          commentId: { type: 'string', description: 'Comment ID to get attachments from' },
          versions: { type: 'boolean', description: 'Include all versions' },
          createdDateStart: { type: 'string', description: 'Created date range begin' },
          createdDateEnd: { type: 'string', description: 'Created date range end' },
        },
      },
      handler: async (params: Record<string, unknown>) => {
        let endpoint = '/attachments';
        
        if (params.taskId) {
          endpoint = `/tasks/${params.taskId}/attachments`;
        } else if (params.folderId) {
          endpoint = `/folders/${params.folderId}/attachments`;
        } else if (params.commentId) {
          endpoint = `/comments/${params.commentId}/attachments`;
        }
        
        const queryParams: Record<string, unknown> = {};
        if (params.versions !== undefined) queryParams.versions = params.versions;
        
        if (params.createdDateStart || params.createdDateEnd) {
          queryParams.createdDate = {
            start: params.createdDateStart,
            end: params.createdDateEnd,
          };
        }
        
        const response = await client.get<WrikeAttachment>(endpoint, queryParams);
        
        return {
          content: [
            {
              type: 'text',
              text: JSON.stringify(response.data, null, 2),
            },
          ],
        };
      },
    },

    // Get attachment by ID
    wrike_get_attachment: {
      name: 'wrike_get_attachment',
      description: 'Get a specific attachment by ID',
      inputSchema: {
        type: 'object',
        properties: {
          attachmentId: { type: 'string', description: 'Attachment ID (required)' },
        },
        required: ['attachmentId'],
      },
      handler: async (params: { attachmentId: string }) => {
        const response = await client.get<WrikeAttachment>(`/attachments/${params.attachmentId}`);
        
        return {
          content: [
            {
              type: 'text',
              text: JSON.stringify(response.data[0], null, 2),
            },
          ],
        };
      },
    },

    // Download attachment
    wrike_download_attachment: {
      name: 'wrike_download_attachment',
      description: 'Get download URL for an attachment',
      inputSchema: {
        type: 'object',
        properties: {
          attachmentId: { type: 'string', description: 'Attachment ID (required)' },
        },
        required: ['attachmentId'],
      },
      handler: async (params: { attachmentId: string }) => {
        const response = await client.get<WrikeAttachment>(`/attachments/${params.attachmentId}/download`);
        
        return {
          content: [
            {
              type: 'text',
              text: JSON.stringify(response.data[0], null, 2),
            },
          ],
        };
      },
    },

    // Get attachment preview URL
    wrike_get_attachment_preview: {
      name: 'wrike_get_attachment_preview',
      description: 'Get preview URL for an attachment',
      inputSchema: {
        type: 'object',
        properties: {
          attachmentId: { type: 'string', description: 'Attachment ID (required)' },
        },
        required: ['attachmentId'],
      },
      handler: async (params: { attachmentId: string }) => {
        const response = await client.get<WrikeAttachment>(`/attachments/${params.attachmentId}/preview`);
        
        return {
          content: [
            {
              type: 'text',
              text: JSON.stringify(response.data[0], null, 2),
            },
          ],
        };
      },
    },

    // Get public URL for attachment
    wrike_get_attachment_url: {
      name: 'wrike_get_attachment_url',
      description: 'Get public URL for an attachment',
      inputSchema: {
        type: 'object',
        properties: {
          attachmentId: { type: 'string', description: 'Attachment ID (required)' },
        },
        required: ['attachmentId'],
      },
      handler: async (params: { attachmentId: string }) => {
        const response = await client.get<WrikeAttachment>(`/attachments/${params.attachmentId}/url`);
        
        return {
          content: [
            {
              type: 'text',
              text: JSON.stringify(response.data[0], null, 2),
            },
          ],
        };
      },
    },

    // Update attachment
    wrike_update_attachment: {
      name: 'wrike_update_attachment',
      description: 'Update attachment name',
      inputSchema: {
        type: 'object',
        properties: {
          attachmentId: { type: 'string', description: 'Attachment ID (required)' },
          name: { type: 'string', description: 'New attachment name (required)' },
        },
        required: ['attachmentId', 'name'],
      },
      handler: async (params: { attachmentId: string; name: string }) => {
        const body = { name: params.name };
        const response = await client.put<WrikeAttachment>(`/attachments/${params.attachmentId}`, body);
        
        return {
          content: [
            {
              type: 'text',
              text: JSON.stringify(response.data[0], null, 2),
            },
          ],
        };
      },
    },

    // Delete attachment
    wrike_delete_attachment: {
      name: 'wrike_delete_attachment',
      description: 'Delete an attachment',
      inputSchema: {
        type: 'object',
        properties: {
          attachmentId: { type: 'string', description: 'Attachment ID (required)' },
        },
        required: ['attachmentId'],
      },
      handler: async (params: { attachmentId: string }) => {
        const response = await client.delete<WrikeAttachment>(`/attachments/${params.attachmentId}`);
        
        return {
          content: [
            {
              type: 'text',
              text: JSON.stringify(response.data[0], null, 2),
            },
          ],
        };
      },
    },
  };
}
