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

vi.mock('vue-router')
vi.mock('@/api/exceptionLog', () => ({
  exceptionLogApi: {
    getPage: vi.fn().mockResolvedValue({
      content: [
        { id: 1, username: 'admin', operation: '用户登录', method: 'POST /api/auth/login', errorMsg: 'NullPointerException', ip: '192.168.1.1', createTime: '2026-01-01T10:00:00' },
        { id: 2, username: 'user', operation: '文件上传', method: 'POST /api/files/upload', errorMsg: 'FileSizeLimitExceededException', ip: '192.168.1.2', createTime: '2026-01-02T11:00:00' },
      ],
      totalElements: 2,
    }),
  },
}))

describe('ExceptionLog 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 exception log container', () => {
      wrapper = mount(ExceptionLog, {
        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-pagination': true,
            'el-dialog': true,
            'el-descriptions': true,
            'el-descriptions-item': true,
          },
        },
      })

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

    it('should initialize with empty search keyword', () => {
      wrapper = mount(ExceptionLog, {
        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-pagination': true,
            'el-dialog': true,
            'el-descriptions': true,
            'el-descriptions-item': true,
          },
        },
      })

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

    it('should initialize with correct pagination defaults', () => {
      wrapper = mount(ExceptionLog, {
        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-pagination': true,
            'el-dialog': true,
            'el-descriptions': true,
            'el-descriptions-item': true,
          },
        },
      })

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

    it('should initialize with hidden detail dialog', () => {
      wrapper = mount(ExceptionLog, {
        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-pagination': true,
            'el-dialog': true,
            'el-descriptions': true,
            'el-descriptions-item': true,
          },
        },
      })

      expect(wrapper.vm.detailVisible).toBe(false)
    })
  })

  describe('detail view handling', () => {
    beforeEach(() => {
      wrapper = mount(ExceptionLog, {
        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-pagination': true,
            'el-dialog': true,
            'el-descriptions': true,
            'el-descriptions-item': true,
          },
        },
      })
    })

    it('should show detail dialog when viewing exception', () => {
      const exception = {
        id: 1,
        username: 'admin',
        operation: '用户登录',
        method: 'POST /api/auth/login',
        errorMsg: 'NullPointerException',
        ip: '192.168.1.1',
        createTime: '2026-01-01T10:00:00',
      }

      wrapper.vm.handleViewDetail(exception)

      expect(wrapper.vm.detailVisible).toBe(true)
      expect(wrapper.vm.currentDetail).toEqual(exception)
    })

    it('should create a copy of exception data for detail view', () => {
      const exception = {
        id: 1,
        username: 'admin',
      }

      wrapper.vm.handleViewDetail(exception)
      wrapper.vm.currentDetail.username = 'modified'

      expect(exception.username).toBe('admin')
    })
  })

  describe('sort handling', () => {
    beforeEach(() => {
      wrapper = mount(ExceptionLog, {
        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-pagination': true,
            'el-dialog': true,
            'el-descriptions': true,
            'el-descriptions-item': true,
          },
        },
      })
    })

    it('should update sort info on ascending order', () => {
      wrapper.vm.handleSortChange({ prop: 'username', order: 'ascending' })
      expect(wrapper.vm.sortInfo.sort).toBe('username')
      expect(wrapper.vm.sortInfo.order).toBe('asc')
    })

    it('should update sort info on descending order', () => {
      wrapper.vm.handleSortChange({ prop: 'createTime', order: 'descending' })
      expect(wrapper.vm.sortInfo.sort).toBe('createTime')
      expect(wrapper.vm.sortInfo.order).toBe('desc')
    })
  })

  describe('pagination handling', () => {
    beforeEach(() => {
      wrapper = mount(ExceptionLog, {
        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-pagination': true,
            'el-dialog': true,
            'el-descriptions': true,
            'el-descriptions-item': true,
          },
        },
      })
    })

    it('should reset to first page on size change', () => {
      wrapper.vm.pagination.current = 5
      wrapper.vm.handleSizeChange()
      expect(wrapper.vm.pagination.current).toBe(1)
    })

    it('should reset to first page on search', () => {
      wrapper.vm.pagination.current = 5
      wrapper.vm.handleSearch()
      expect(wrapper.vm.pagination.current).toBe(1)
    })
  })
})
