import type {
  MicrosoftTeamsReadResponse,
  MicrosoftTeamsToolParams,
} from '@/tools/microsoft_teams/types'
import {
  downloadAllReferenceAttachments,
  extractMessageAttachments,
  fetchHostedContentsForChatMessage,
} from '@/tools/microsoft_teams/utils'
import type { ToolConfig } from '@/tools/types'

export const readChatTool: ToolConfig<MicrosoftTeamsToolParams, MicrosoftTeamsReadResponse> = {
  id: 'microsoft_teams_read_chat',
  name: 'Read Microsoft Teams Chat',
  description: 'Read content from a Microsoft Teams chat',
  version: '1.0',
  errorExtractor: 'nested-error-object',

  oauth: {
    required: true,
    provider: 'microsoft-teams',
  },

  params: {
    accessToken: {
      type: 'string',
      required: true,
      visibility: 'hidden',
      description: 'The access token for the Microsoft Teams API',
    },
    chatId: {
      type: 'string',
      required: true,
      visibility: 'user-only',
      description: 'The ID of the chat to read from',
    },
    includeAttachments: {
      type: 'boolean',
      required: false,
      visibility: 'user-only',
      description: 'Download and include message attachments (hosted contents) into storage',
    },
  },

  request: {
    url: (params) => {
      const chatId = params.chatId?.trim()
      if (!chatId) {
        throw new Error('Chat ID is required')
      }
      return `https://graph.microsoft.com/v1.0/chats/${encodeURIComponent(chatId)}/messages?$top=50&$orderby=createdDateTime desc`
    },
    method: 'GET',
    headers: (params) => {
      if (!params.accessToken) {
        throw new Error('Access token is required')
      }

      return {
        Authorization: `Bearer ${params.accessToken}`,
      }
    },
  },

  transformResponse: async (response: Response, params?: MicrosoftTeamsToolParams) => {
    const data = await response.json()

    const messages = data.value || []

    if (messages.length === 0) {
      return {
        success: true,
        output: {
          content: 'No messages found in this chat.',
          metadata: {
            chatId: '',
            messageCount: 0,
            messages: [],
            totalAttachments: 0,
            attachmentTypes: [],
          },
        },
      }
    }

    const processedMessages = await Promise.all(
      messages.map(async (message: any) => {
        const content = message.body?.content || 'No content'
        const messageId = message.id

        const attachments = extractMessageAttachments(message)

        let uploaded: any[] = []
        if (params?.includeAttachments && params.accessToken && params.chatId && messageId) {
          try {
            const hostedContents = await fetchHostedContentsForChatMessage({
              accessToken: params.accessToken,
              chatId: params.chatId,
              messageId,
            })
            uploaded.push(...hostedContents)

            const referenceFiles = await downloadAllReferenceAttachments({
              accessToken: params.accessToken,
              attachments,
            })
            uploaded.push(...referenceFiles)
          } catch (_e) {
            uploaded = []
          }
        }

        return {
          id: messageId,
          content: content, // Keep original content without modification
          sender: message.from?.user?.displayName || 'Unknown',
          timestamp: message.createdDateTime,
          messageType: message.messageType || 'message',
          attachments, // Raw attachment metadata
          uploadedFiles: uploaded, // Uploaded file infos (paths/keys)
        }
      })
    )

    // Format the messages into a readable text (no attachment info in content)
    const formattedMessages = processedMessages
      .map((message: any) => {
        const sender = message.sender
        const timestamp = message.timestamp
          ? new Date(message.timestamp).toLocaleString()
          : 'Unknown time'

        return `[${timestamp}] ${sender}: ${message.content}`
      })
      .join('\n\n')

    // Calculate attachment statistics
    const allAttachments = processedMessages.flatMap((msg: any) => msg.attachments || [])
    const attachmentTypes: string[] = []
    const seenTypes = new Set<string>()

    allAttachments.forEach((att: any) => {
      if (
        att.contentType &&
        typeof att.contentType === 'string' &&
        !seenTypes.has(att.contentType)
      ) {
        attachmentTypes.push(att.contentType)
        seenTypes.add(att.contentType)
      }
    })

    // Create document metadata
    const metadata = {
      chatId: messages[0]?.chatId || params?.chatId || '',
      messageCount: messages.length,
      totalAttachments: allAttachments.length,
      attachmentTypes,
      messages: processedMessages,
    }

    // Flatten uploaded files across all messages for convenience
    const flattenedUploads = processedMessages.flatMap((m: any) => m.uploadedFiles || [])

    return {
      success: true,
      output: {
        content: formattedMessages,
        metadata,
        attachments: flattenedUploads,
      },
    }
  },

  outputs: {
    success: { type: 'boolean', description: 'Teams chat read operation success status' },
    messageCount: { type: 'number', description: 'Number of messages retrieved from chat' },
    chatId: { type: 'string', description: 'ID of the chat that was read from' },
    messages: { type: 'array', description: 'Array of chat message objects' },
    attachmentCount: { type: 'number', description: 'Total number of attachments found' },
    attachmentTypes: { type: 'array', description: 'Types of attachments found' },
    content: { type: 'string', description: 'Formatted content of chat messages' },
    attachments: {
      type: 'file[]',
      description: 'Uploaded attachments for convenience (flattened)',
    },
  },
}
