import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest'
import { mount } from '@vue/test-utils'
import { createRouter, createMemoryHistory } from 'vue-router'
import FileManagement from '@/views/file/FileManagement.vue'

vi.mock('vue-router')
vi.mock('element-plus', () => ({
  ElMessage: {
    success: vi.fn(),
    error: vi.fn(),
  },
  ElMessageBox: {
    confirm: vi.fn(),
  },
}))

vi.mock('@/utils/request', () => {
  const mockRequest = {
    get: vi.fn(),
    post: vi.fn(),
    put: vi.fn(),
    delete: vi.fn(),
  }
  
  mockRequest.get.mockResolvedValue([
    { id: 1, fileName: 'test.pdf', fileSize: 1024, fileType: 'application/pdf', storageType: 'local', createdAt: '2026-01-01', createBy: 'admin' },
    { id: 2, fileName: 'image.png', fileSize: 2048, fileType: 'image/png', storageType: 'local', createdAt: '2026-01-02', createBy: 'user' },
  ])
  mockRequest.post.mockResolvedValue({})
  mockRequest.put.mockResolvedValue({})
  mockRequest.delete.mockResolvedValue({})
  
  return {
    default: mockRequest,
  }
})

describe('FileManagement Component', () => {
  let router: any
  let wrapper: any

  beforeEach(() => {
    router = createRouter({
      history: createMemoryHistory(),
      routes: [
        { path: '/', component: { template: '<div>Home</div>' } },
      ],
    })

    vi.clearAllMocks()
  })

  afterEach(() => {
    if (wrapper) {
      wrapper.unmount()
    }
  })

  describe('component initialization', () => {
    it('should render file management container', () => {
      wrapper = mount(FileManagement, {
        global: {
          plugins: [router],
          stubs: {
            'el-card': true,
            'el-button': true,
            'el-table': true,
            'el-table-column': true,
            'el-input': true,
            'el-upload': true,
            'el-tag': true,
            'el-icon': true,
          },
        },
      })

      expect(wrapper.find('.file-management').exists()).toBe(true)
    })

    it('should initialize with empty search keyword', () => {
      wrapper = mount(FileManagement, {
        global: {
          plugins: [router],
          stubs: {
            'el-card': true,
            'el-button': true,
            'el-table': true,
            'el-table-column': true,
            'el-input': true,
            'el-upload': true,
            'el-tag': true,
            'el-icon': true,
          },
        },
      })

      expect(wrapper.vm.searchKeyword).toBe('')
    })

    it('should initialize with loading state false before data fetch', async () => {
      wrapper = mount(FileManagement, {
        global: {
          plugins: [router],
          stubs: {
            'el-card': true,
            'el-button': true,
            'el-table': true,
            'el-table-column': true,
            'el-input': true,
            'el-upload': true,
            'el-tag': true,
            'el-icon': true,
          },
        },
      })

      await wrapper.vm.$nextTick()
      expect([true, false]).toContain(wrapper.vm.loading)
    })
  })

  describe('file type utilities', () => {
    beforeEach(() => {
      wrapper = mount(FileManagement, {
        global: {
          plugins: [router],
          stubs: {
            'el-card': true,
            'el-button': true,
            'el-table': true,
            'el-table-column': true,
            'el-input': true,
            'el-upload': true,
            'el-tag': true,
            'el-icon': true,
          },
        },
      })
    })

    it('should return correct file type name for images', () => {
      expect(wrapper.vm.getFileTypeName('image/png')).toBe('图片')
      expect(wrapper.vm.getFileTypeName('image/jpeg')).toBe('图片')
    })

    it('should return correct file type name for videos', () => {
      expect(wrapper.vm.getFileTypeName('video/mp4')).toBe('视频')
    })

    it('should return correct file type name for audio', () => {
      expect(wrapper.vm.getFileTypeName('audio/mp3')).toBe('音频')
    })

    it('should return correct file type name for PDF', () => {
      expect(wrapper.vm.getFileTypeName('application/pdf')).toBe('PDF')
    })

    it('should return correct file type name for Word', () => {
      expect(wrapper.vm.getFileTypeName('application/vnd.openxmlformats-officedocument.wordprocessingml.document')).toBe('Word')
    })

    it('should return correct file type name for Excel', () => {
      expect(wrapper.vm.getFileTypeName('application/vnd.ms-excel')).toBe('Excel')
    })

    it('should return unknown for unknown file types', () => {
      expect(wrapper.vm.getFileTypeName('')).toBe('未知')
      expect(wrapper.vm.getFileTypeName('unknown/type')).toBe('其他')
    })

    it('should return correct tag type for images', () => {
      expect(wrapper.vm.getFileTypeTag('image/png')).toBe('success')
    })

    it('should return correct tag type for videos', () => {
      expect(wrapper.vm.getFileTypeTag('video/mp4')).toBe('danger')
    })

    it('should return correct tag type for audio', () => {
      expect(wrapper.vm.getFileTypeTag('audio/mp3')).toBe('warning')
    })

    it('should return correct tag type for PDF', () => {
      expect(wrapper.vm.getFileTypeTag('application/pdf')).toBe('danger')
    })

    it('should return correct tag type for unknown', () => {
      expect(wrapper.vm.getFileTypeTag('')).toBe('info')
    })
  })

  describe('search functionality', () => {
    beforeEach(() => {
      wrapper = mount(FileManagement, {
        global: {
          plugins: [router],
          stubs: {
            'el-card': true,
            'el-button': true,
            'el-table': true,
            'el-table-column': true,
            'el-input': true,
            'el-upload': true,
            'el-tag': true,
            'el-icon': true,
          },
        },
      })
    })

    it('should filter files by search keyword', async () => {
      wrapper.vm.dataSource = [
        { id: 1, fileName: 'test.pdf' },
        { id: 2, fileName: 'image.png' },
        { id: 3, fileName: 'document.doc' },
      ]
      
      wrapper.vm.searchKeyword = 'test'
      await wrapper.vm.$nextTick()
      
      expect(wrapper.vm.filteredDataSource.length).toBe(1)
      expect(wrapper.vm.filteredDataSource[0].fileName).toBe('test.pdf')
    })

    it('should return all files when search keyword is empty', () => {
      wrapper.vm.dataSource = [
        { id: 1, fileName: 'test.pdf' },
        { id: 2, fileName: 'image.png' },
      ]
      
      wrapper.vm.searchKeyword = ''
      
      expect(wrapper.vm.filteredDataSource.length).toBe(2)
    })

    it('should be case insensitive when searching', () => {
      wrapper.vm.dataSource = [
        { id: 1, fileName: 'TEST.pdf' },
        { id: 2, fileName: 'image.png' },
      ]
      
      wrapper.vm.searchKeyword = 'test'
      
      expect(wrapper.vm.filteredDataSource.length).toBe(1)
    })
  })
})
