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

vi.mock('vue-router')
vi.mock('@/utils/request', () => {
  const mockRequest = {
    get: vi.fn().mockResolvedValue({
      content: [
        { id: 1, username: 'admin', ip: '192.168.1.1', location: '北京', browser: 'Chrome', os: 'Windows', status: '0', loginTime: '2026-01-01T10:00:00' },
        { id: 2, username: 'user', ip: '192.168.1.2', location: '上海', browser: 'Firefox', os: 'MacOS', status: '1', loginTime: '2026-01-02T11:00:00' },
      ],
      totalElements: 2,
    }),
    post: vi.fn(),
    put: vi.fn(),
    delete: vi.fn(),
  }
  
  return {
    default: mockRequest,
  }
})

describe('LoginLog 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 login log container', () => {
      wrapper = mount(LoginLog, {
        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,
          },
        },
      })

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

    it('should initialize with empty search keyword', () => {
      wrapper = mount(LoginLog, {
        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,
          },
        },
      })

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

    it('should initialize with correct pagination defaults', () => {
      wrapper = mount(LoginLog, {
        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,
          },
        },
      })

      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 correct sort defaults', () => {
      wrapper = mount(LoginLog, {
        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,
          },
        },
      })

      expect(wrapper.vm.sortInfo.sort).toBe('id')
      expect(wrapper.vm.sortInfo.order).toBe('asc')
    })
  })

  describe('sort handling', () => {
    beforeEach(() => {
      wrapper = mount(LoginLog, {
        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,
          },
        },
      })
    })

    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: 'loginTime', order: 'descending' })
      expect(wrapper.vm.sortInfo.sort).toBe('loginTime')
      expect(wrapper.vm.sortInfo.order).toBe('desc')
    })
  })

  describe('pagination handling', () => {
    beforeEach(() => {
      wrapper = mount(LoginLog, {
        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,
          },
        },
      })
    })

    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)
    })
  })
})
