import { z } from 'zod'
import type { Tool } from '../../../../types'
import { createIntegrationTool } from '../../../tool'

/**
 * Create a tool that uploads files to Dropbox
 *
 * @param credentialName - Name of the Dropbox credential to use
 * @returns Tool instance
 *
 * @example
 * ```typescript
 * const tool = createDropboxUploadTool('elevasis-dropbox')
 * const result = await tool.execute({
 *   input: {
 *     path: '/Clients/Acme/Contracts/proposal.pdf',
 *     contents: pdfBuffer,
 *     mode: 'overwrite'
 *   },
 *   executionContext: context
 * })
 * // result: { id: 'id:xxx', path_display: '/Clients/Acme/Contracts/proposal.pdf', ... }
 * ```
 */
export function createDropboxUploadTool(credentialName: string): Tool {
  return createIntegrationTool({
    name: 'dropbox_upload_file',
    description: `Upload a file to Dropbox.

Use this tool to store files in Dropbox. Supports uploading PDFs, documents,
and other files up to 150MB.

Write modes:
- add: Do not overwrite if file exists (default)
- overwrite: Always overwrite existing file

The file will be uploaded to the specified path, creating parent folders if needed.`,
    inputSchema: z.object({
      path: z.string().describe('Full path for the file (e.g., /Clients/Acme/contract.pdf)'),
      contents: z.instanceof(Buffer).describe('File contents as Buffer'),
      mode: z.enum(['add', 'overwrite']).optional().describe('Write mode (default: overwrite)'),
      autorename: z.boolean().optional().describe('Auto-rename if conflict (default: false)')
    }),
    outputSchema: z.object({
      id: z.string().describe('Dropbox file ID'),
      name: z.string().describe('File name'),
      path_lower: z.string().describe('Lowercase path'),
      path_display: z.string().describe('Display path with original casing'),
      rev: z.string().describe('File revision'),
      size: z.number().describe('File size in bytes'),
      content_hash: z.string().optional().describe('Content hash for verification')
    }),
    integration: 'dropbox' as const,
    method: 'uploadFile' as const,
    credentialName
  })
}

/**
 * Create a tool that creates folders in Dropbox
 *
 * @param credentialName - Name of the Dropbox credential to use
 * @returns Tool instance
 *
 * @example
 * ```typescript
 * const tool = createDropboxCreateFolderTool('elevasis-dropbox')
 * const result = await tool.execute({
 *   input: {
 *     path: '/Clients/Acme/Contracts'
 *   },
 *   executionContext: context
 * })
 * // result: { metadata: { id: 'id:xxx', path_display: '/Clients/Acme/Contracts' } } or { metadata: null } if exists
 * ```
 */
export function createDropboxCreateFolderTool(credentialName: string): Tool {
  return createIntegrationTool({
    name: 'dropbox_create_folder',
    description: `Create a folder in Dropbox.

Use this tool to create folder structures. If the folder already exists,
returns null metadata (not an error).

Note: This only creates a single folder level. For nested paths like
/Clients/Acme/Contracts, you need to create each level separately.`,
    inputSchema: z.object({
      path: z.string().describe('Full path for the folder (e.g., /Clients/Acme)'),
      autorename: z.boolean().optional().describe('Auto-rename if conflict (default: false)')
    }),
    outputSchema: z.object({
      metadata: z
        .object({
          id: z.string().describe('Dropbox folder ID'),
          name: z.string().describe('Folder name'),
          path_lower: z.string().describe('Lowercase path'),
          path_display: z.string().describe('Display path with original casing')
        })
        .nullable()
        .describe('Folder metadata, or null if folder already exists')
    }),
    integration: 'dropbox' as const,
    method: 'createFolder' as const,
    credentialName
  })
}
