/**
 * Converter for "file" message type.
 */

import type { ConvertedPayload, MessageConvertContext } from './types'
import { safeParse } from './utils'

export async function convertFile(ctx: MessageConvertContext): Promise<ConvertedPayload | null> {
  const parsed = safeParse(ctx.rawContent) as { file_key?: string; file_name?: string } | undefined
  const fileKey = parsed?.file_key
  if (!fileKey) return null

  const fileData = await ctx.downloadResource(fileKey, 'file')
  return {
    text: '',
    files: [{
      data: fileData,
      name: parsed?.file_name || 'file',
      mimeType: 'application/octet-stream',
      size: fileData.length,
    }],
  }
}
