import type { Topic } from '@renderer/types/topic'
import { beforeEach, describe, expect, it, vi } from 'vitest'

import { CONTENT_TYPES, processMessageContent } from '../knowledge'

// Mock modules to prevent circular dependencies during test loading
vi.mock('@renderer/components/Popups/SaveToKnowledgePopup', () => ({
  default: {}
}))

vi.mock('@renderer/components/chat/messages/frame/MessageMenuBar', () => ({
  default: {}
}))

// Simple mocks
vi.mock('@renderer/hooks/useTopic', () => ({
  getTopicMessages: vi.fn()
}))

describe('Topic Knowledge Functions', () => {
  beforeEach(() => {
    vi.clearAllMocks()
  })

  const createTestTopic = (): Topic => ({
    id: 'test-topic-1',
    assistantId: 'test-assistant',
    name: 'Test Topic',
    createdAt: '2023-01-01T00:00:00Z',
    updatedAt: '2023-01-01T00:00:00Z',
    messages: []
  })

  describe('CONTENT_TYPES', () => {
    it('should have all expected content types', () => {
      expect(CONTENT_TYPES.TEXT).toBe('text')
      expect(CONTENT_TYPES.CODE).toBe('code')
      expect(CONTENT_TYPES.THINKING).toBe('thinking')
      expect(CONTENT_TYPES.TOOL_USE).toBe('tools')
      expect(CONTENT_TYPES.CITATION).toBe('citations')
      expect(CONTENT_TYPES.TRANSLATION).toBe('translations')
      expect(CONTENT_TYPES.ERROR).toBe('errors')
      expect(CONTENT_TYPES.FILE).toBe('files')
      expect(CONTENT_TYPES.IMAGES).toBe('images')
    })
  })

  describe('Topic Data Structure', () => {
    it('should create valid topic structure', () => {
      const topic = createTestTopic()

      expect(topic).toHaveProperty('id')
      expect(topic).toHaveProperty('name')
      expect(topic).toHaveProperty('assistantId')
      expect(topic).toHaveProperty('createdAt')
      expect(topic).toHaveProperty('updatedAt')
      expect(topic).toHaveProperty('messages')
      expect(Array.isArray(topic.messages)).toBe(true)
    })
  })

  describe('Topic Knowledge Functions Integration', () => {
    it('should be importable without circular dependencies', async () => {
      // This test verifies that the knowledge functions can be imported
      // without causing circular dependency issues
      const knowledgeModule = await import('../knowledge')

      expect(knowledgeModule).toHaveProperty('analyzeTopicContent')
      expect(knowledgeModule).toHaveProperty('processTopicContent')
      expect(knowledgeModule).toHaveProperty('CONTENT_TYPES')
      expect(typeof knowledgeModule.analyzeTopicContent).toBe('function')
      expect(typeof knowledgeModule.processTopicContent).toBe('function')
    })

    it('should handle getTopicMessages mock correctly', async () => {
      const { getTopicMessages } = await import('@renderer/hooks/useTopic')
      expect(typeof getTopicMessages).toBe('function')
    })
  })

  describe('processMessageContent', () => {
    it('converts file part URLs to absolute filesystem paths for file metadata', () => {
      const result = processMessageContent(
        {
          id: 'message-1',
          role: 'user',
          topicId: 'topic-1',
          createdAt: '2026-01-01T00:00:00.000Z',
          status: 'success',
          parts: [
            {
              type: 'file',
              url: 'file:///tmp/report%20final.pdf',
              filename: 'report final.pdf',
              mediaType: 'application/pdf'
            }
          ]
        },
        [CONTENT_TYPES.FILE]
      )

      expect(result.files).toEqual([
        expect.objectContaining({
          name: 'report final.pdf',
          path: '/tmp/report final.pdf',
          type: 'application/pdf'
        })
      ])
    })
  })
})
