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

vi.mock('vue-router')
vi.mock('@/api/operationLog', () => ({
  operationLogApi: {
    getPage: vi.fn().mockResolvedValue({
      content: [
        { id: 1, username: 'admin', operation: '用户登录', method: 'POST', params: '{}', status: '0', duration: 100, createdAt: '2026-01-01T10:00:00' },
        { id: 2, username: 'user', operation: '查看用户', method: 'GET', params: '{"id":1}', status: '0', duration: 50, createdAt: '2026-01-02T11:00:00' },
      ],
      totalElements: 2,
    }),
  },
}))

describe('OperationLog 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 operation log container', () => {
      wrapper = mount(OperationLog, {
        global: {
          plugins: [router],
          stubs: {
            'el-card': true,
            'el-button': true,
            'el-table': true,
            'el-table-column': true,
            'el-input': true,
            'el-tag': true,
            'el-icon': true,
            'el-popover': true,
            'el-pagination': true,
          },
        },
      })

      expect(wrapper.find('.operation-log').exists()).toBe(true)
    })

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

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

    it('should initialize with correct pagination defaults', () => {
      wrapper = mount(OperationLog, {
        global: {
          plugins: [router],
          stubs: {
            'el-card': true,
            'el-button': true,
            'el-table': true,
            'el-table-column': true,
            'el-input': true,
            'el-tag': true,
            'el-icon': true,
            'el-popover': true,
            'el-pagination': true,
          },
        },
      })

      expect(wrapper.vm.pagination.current).toBe(1)
      expect(wrapper.vm.pagination.pageSize).toBe(10)
      expect(wrapper.vm.pagination.total).toBe(0)
    })
  })

  describe('operation icon mapping', () => {
    beforeEach(() => {
      wrapper = mount(OperationLog, {
        global: {
          plugins: [router],
          stubs: {
            'el-card': true,
            'el-button': true,
            'el-table': true,
            'el-table-column': true,
            'el-input': true,
            'el-tag': true,
            'el-icon': true,
            'el-popover': true,
            'el-pagination': true,
          },
        },
      })
    })

    it('should return User icon for login operations', () => {
      const icon = wrapper.vm.getOperationIcon('用户登录')
      expect(icon.name).toBe('User')
    })

    it('should return Delete icon for delete operations', () => {
      const icon = wrapper.vm.getOperationIcon('删除用户')
      expect(icon.name).toBe('Delete')
    })

    it('should return Edit icon for update operations', () => {
      const icon = wrapper.vm.getOperationIcon('编辑用户')
      expect(icon.name).toBe('Edit')
    })

    it('should return View icon for view operations', () => {
      const icon = wrapper.vm.getOperationIcon('查看用户')
      expect(icon.name).toBe('View')
    })

    it('should return Plus icon for create operations', () => {
      const icon = wrapper.vm.getOperationIcon('新增用户')
      expect(icon.name).toBe('Plus')
    })

    it('should return Download icon for download operations', () => {
      const icon = wrapper.vm.getOperationIcon('下载文件')
      expect(icon.name).toBe('Download')
    })

    it('should return Setting icon for config operations', () => {
      const icon = wrapper.vm.getOperationIcon('系统设置')
      expect(icon.name).toBe('Setting')
    })

    it('should return Lock icon for password operations', () => {
      const icon = wrapper.vm.getOperationIcon('重置密码')
      expect(icon.name).toBe('Lock')
    })

    it('should return Document icon for unknown operations', () => {
      const icon = wrapper.vm.getOperationIcon('未知操作')
      expect(icon.name).toBe('Document')
    })
  })

  describe('params formatting', () => {
    beforeEach(() => {
      wrapper = mount(OperationLog, {
        global: {
          plugins: [router],
          stubs: {
            'el-card': true,
            'el-button': true,
            'el-table': true,
            'el-table-column': true,
            'el-input': true,
            'el-tag': true,
            'el-icon': true,
            'el-popover': true,
            'el-pagination': true,
          },
        },
      })
    })

    it('should format valid JSON params', () => {
      const params = '{"name":"test","id":1}'
      const formatted = wrapper.vm.formatParams(params)
      expect(formatted).toContain('name')
      expect(formatted).toContain('test')
    })

    it('should return empty string for null params', () => {
      const formatted = wrapper.vm.formatParams(null)
      expect(formatted).toBe('')
    })

    it('should return empty string for undefined params', () => {
      const formatted = wrapper.vm.formatParams(undefined)
      expect(formatted).toBe('')
    })

    it('should return original string for invalid JSON', () => {
      const params = 'not a json'
      const formatted = wrapper.vm.formatParams(params)
      expect(formatted).toBe('not a json')
    })
  })
})
