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

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

vi.mock('@/api/user.api', () => ({
  userApi: {
    getPage: vi.fn(),
    create: vi.fn(),
    update: vi.fn(),
    delete: vi.fn(),
    assignRoles: vi.fn(),
  },
}))

vi.mock('@/api/role.api', () => ({
  roleApi: {
    getAll: vi.fn(),
  },
}))

describe('UserManagement 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 user management container', () => {
      wrapper = mount(UserManagement, {
        global: {
          plugins: [router],
          stubs: {
            'el-card': true,
            'el-input': true,
            'el-button': true,
            'el-table': true,
            'el-table-column': true,
            'el-tag': true,
            'el-pagination': true,
            'el-dialog': true,
            'el-form': true,
            'el-form-item': true,
            'el-select': true,
            'el-option': true,
            'el-icon': true,
          },
        },
      })

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

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

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

    it('should initialize with loading state false before data fetch', () => {
      wrapper = mount(UserManagement, {
        global: {
          plugins: [router],
          stubs: {
            'el-card': true,
            'el-input': true,
            'el-button': true,
            'el-table': true,
            'el-table-column': true,
            'el-tag': true,
            'el-pagination': true,
            'el-dialog': true,
            'el-form': true,
            'el-form-item': true,
            'el-select': true,
            'el-option': true,
            'el-icon': true,
          },
        },
      })

      expect(wrapper.vm.loading).toBeDefined()
      expect(typeof wrapper.vm.loading).toBe('boolean')
    })

    it('should initialize with empty data source', () => {
      wrapper = mount(UserManagement, {
        global: {
          plugins: [router],
          stubs: {
            'el-card': true,
            'el-input': true,
            'el-button': true,
            'el-table': true,
            'el-table-column': true,
            'el-tag': true,
            'el-pagination': true,
            'el-dialog': true,
            'el-form': true,
            'el-form-item': true,
            'el-select': true,
            'el-option': true,
            'el-icon': true,
          },
        },
      })

      expect(wrapper.vm.dataSource).toEqual([])
    })

    it('should initialize with pagination on page 1', () => {
      wrapper = mount(UserManagement, {
        global: {
          plugins: [router],
          stubs: {
            'el-card': true,
            'el-input': true,
            'el-button': true,
            'el-table': true,
            'el-table-column': true,
            'el-tag': true,
            'el-pagination': true,
            'el-dialog': true,
            'el-form': true,
            'el-form-item': true,
            'el-select': true,
            'el-option': true,
            'el-icon': true,
          },
        },
      })

      expect(wrapper.vm.pagination.current).toBe(1)
    })

    it('should initialize with modal visible false', () => {
      wrapper = mount(UserManagement, {
        global: {
          plugins: [router],
          stubs: {
            'el-card': true,
            'el-input': true,
            'el-button': true,
            'el-table': true,
            'el-table-column': true,
            'el-tag': true,
            'el-pagination': true,
            'el-dialog': true,
            'el-form': true,
            'el-form-item': true,
            'el-select': true,
            'el-option': true,
            'el-icon': true,
          },
        },
      })

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

    it('should initialize with empty form state', () => {
      wrapper = mount(UserManagement, {
        global: {
          plugins: [router],
          stubs: {
            'el-card': true,
            'el-input': true,
            'el-button': true,
            'el-table': true,
            'el-table-column': true,
            'el-tag': true,
            'el-pagination': true,
            'el-dialog': true,
            'el-form': true,
            'el-form-item': true,
            'el-select': true,
            'el-option': true,
            'el-icon': true,
          },
        },
      })

      expect(wrapper.vm.formState.username).toBe('')
      expect(wrapper.vm.formState.password).toBe('')
      expect(wrapper.vm.formState.nickname).toBe('')
      expect(wrapper.vm.formState.email).toBe('')
      expect(wrapper.vm.formState.phone).toBe('')
      expect(wrapper.vm.formState.roles).toEqual([])
    })
  })

  describe('search functionality', () => {
    it('should have handleSearch method', () => {
      wrapper = mount(UserManagement, {
        global: {
          plugins: [router],
          stubs: {
            'el-card': true,
            'el-input': true,
            'el-button': true,
            'el-table': true,
            'el-table-column': true,
            'el-tag': true,
            'el-pagination': true,
            'el-dialog': true,
            'el-form': true,
            'el-form-item': true,
            'el-select': true,
            'el-option': true,
            'el-icon': true,
          },
        },
      })

      expect(typeof wrapper.vm.handleSearch).toBe('function')
    })

    it('should update search keyword when input changes', async () => {
      wrapper = mount(UserManagement, {
        global: {
          plugins: [router],
          stubs: {
            'el-card': true,
            'el-input': true,
            'el-button': true,
            'el-table': true,
            'el-table-column': true,
            'el-tag': true,
            'el-pagination': true,
            'el-dialog': true,
            'el-form': true,
            'el-form-item': true,
            'el-select': true,
            'el-option': true,
            'el-icon': true,
          },
        },
      })

      wrapper.vm.searchKeyword = 'testuser'
      await wrapper.vm.$nextTick()

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

  describe('add user functionality', () => {
    it('should have handleAdd method', () => {
      wrapper = mount(UserManagement, {
        global: {
          plugins: [router],
          stubs: {
            'el-card': true,
            'el-input': true,
            'el-button': true,
            'el-table': true,
            'el-table-column': true,
            'el-tag': true,
            'el-pagination': true,
            'el-dialog': true,
            'el-form': true,
            'el-form-item': true,
            'el-select': true,
            'el-option': true,
            'el-icon': true,
          },
        },
      })

      expect(typeof wrapper.vm.handleAdd).toBe('function')
    })
  })

  describe('pagination functionality', () => {
    it('should have handleTableChange method', () => {
      wrapper = mount(UserManagement, {
        global: {
          plugins: [router],
          stubs: {
            'el-card': true,
            'el-input': true,
            'el-button': true,
            'el-table': true,
            'el-table-column': true,
            'el-tag': true,
            'el-pagination': true,
            'el-dialog': true,
            'el-form': true,
            'el-form-item': true,
            'el-select': true,
            'el-option': true,
            'el-icon': true,
          },
        },
      })

      expect(typeof wrapper.vm.handleTableChange).toBe('function')
    })

    it('should have handleSizeChange method', () => {
      wrapper = mount(UserManagement, {
        global: {
          plugins: [router],
          stubs: {
            'el-card': true,
            'el-input': true,
            'el-button': true,
            'el-table': true,
            'el-table-column': true,
            'el-tag': true,
            'el-pagination': true,
            'el-dialog': true,
            'el-form': true,
            'el-form-item': true,
            'el-select': true,
            'el-option': true,
            'el-icon': true,
          },
        },
      })

      expect(typeof wrapper.vm.handleSizeChange).toBe('function')
    })
  })

  describe('sort functionality', () => {
    it('should have handleSortChange method', () => {
      wrapper = mount(UserManagement, {
        global: {
          plugins: [router],
          stubs: {
            'el-card': true,
            'el-input': true,
            'el-button': true,
            'el-table': true,
            'el-table-column': true,
            'el-tag': true,
            'el-pagination': true,
            'el-dialog': true,
            'el-form': true,
            'el-form-item': true,
            'el-select': true,
            'el-option': true,
            'el-icon': true,
          },
        },
      })

      expect(typeof wrapper.vm.handleSortChange).toBe('function')
    })

    it('should initialize with default sort info', () => {
      wrapper = mount(UserManagement, {
        global: {
          plugins: [router],
          stubs: {
            'el-card': true,
            'el-input': true,
            'el-button': true,
            'el-table': true,
            'el-table-column': true,
            'el-tag': true,
            'el-pagination': true,
            'el-dialog': true,
            'el-form': true,
            'el-form-item': true,
            'el-select': true,
            'el-option': true,
            'el-icon': true,
          },
        },
      })

      expect(wrapper.vm.sortInfo.sortBy).toBe('id')
      expect(wrapper.vm.sortInfo.sortOrder).toBe('asc')
    })
  })
})
