import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'
import { setActivePinia, createPinia } from 'pinia'
import { mount, flushPromises } from '@vue/test-utils'
import { nextTick } from 'vue'
import { message } from 'ant-design-vue'
import { ApiError } from '@/types/api'
import { useUserStore } from '@/stores/user'

// jsdom 不支持 matchMedia，Ant Design Vue 响应式布局需要
Object.defineProperty(window, 'matchMedia', {
  writable: true,
  value: (query: string) => ({
    matches: false,
    media: query,
    onchange: null,
    addListener: () => {},
    removeListener: () => {},
    addEventListener: () => {},
    removeEventListener: () => {},
    dispatchEvent: () => false,
  }),
})

vi.mock('@/utils/request', () => ({
  api: {
    get: vi.fn(),
    post: vi.fn(),
  },
}))

const mockPush = vi.fn()

vi.mock('vue-router', () => ({
  useRouter: () => ({ push: mockPush }),
  useRoute: () => ({ query: {} }),
}))

vi.mock('@/components/Brand.vue', () => ({
  default: { template: '<div class="brand-stub" />' },
}))

function getSetupState(w: ReturnType<typeof mount>) {
  // @ts-expect-error setupState is Vue internal, not in public types
  return w.getCurrentComponent().setupState
}

describe('Register Page', () => {
  let wrapper: ReturnType<typeof mount>

  beforeEach(() => {
    setActivePinia(createPinia())
    localStorage.clear()
    vi.restoreAllMocks()
    mockPush.mockReset()
    vi.useFakeTimers()
  })

  afterEach(() => {
    wrapper?.unmount()
    vi.useRealTimers()
  })

  async function mountPage() {
    const { default: RegisterPage } = await import('../register.vue')

    wrapper = mount(RegisterPage, {
      attachTo: document.body,
      global: {
        stubs: {
          UserOutlined: { template: '<span />' },
          LockOutlined: { template: '<span />' },
          MailOutlined: { template: '<span />' },
        },
      },
    })
    await flushPromises()
    await nextTick()
    return wrapper
  }

  it('renders register form with all fields and submit button', async () => {
    await mountPage()
    const html = wrapper.html()
    // 验证所有输入框的 placeholder 存在
    expect(html).toContain('请输入用户名')
    expect(html).toContain('请输入邮箱地址')
    expect(html).toContain('请输入密码')
    expect(html).toContain('请再次输入密码')
    // 验证注册按钮存在
    expect(html).toContain('注册')
  })

  it('calls /api/v1/register and redirects to /login on success without token', async () => {
    const { api } = await import('@/utils/request')
    vi.mocked(api.post).mockResolvedValueOnce({})
    const successSpy = vi.spyOn(message, 'success').mockImplementation(() => ({}) as never)

    await mountPage()
    const store = useUserStore()
    const vm = getSetupState(wrapper)
    vm.formState.username = 'newuser'
    vm.formState.email = 'new@test.com'
    vm.formState.password = 'password123'
    vm.formState.confirmPassword = 'password123'
    vm.formRef = { validate: vi.fn().mockResolvedValue(true) }
    await vm.handleSubmit()
    await flushPromises()

    expect(api.post).toHaveBeenCalledWith('/api/v1/register', {
      username: 'newuser',
      email: 'new@test.com',
      password: 'password123',
    })
    expect(store.token).toBeNull()
    expect(successSpy).toHaveBeenCalled()

    vi.advanceTimersByTime(1500)
    expect(mockPush).toHaveBeenCalledWith('/login')
    successSpy.mockRestore()
  })

  it('sets token and user when backend returns them on register', async () => {
    const { api } = await import('@/utils/request')
    const mockResponse = {
      access_token: 'a.b.c',
      refresh_token: 'r.r.r',
      user: { id: 2, username: 'newuser', email: 'new@test.com', role: 'user', status: 1 },
    }
    vi.mocked(api.post).mockResolvedValueOnce(mockResponse)
    vi.spyOn(message, 'success').mockImplementation(() => ({}) as never)

    await mountPage()
    const store = useUserStore()
    const vm = getSetupState(wrapper)
    vm.formState.username = 'newuser'
    vm.formState.email = 'new@test.com'
    vm.formState.password = 'password123'
    vm.formState.confirmPassword = 'password123'
    vm.formRef = { validate: vi.fn().mockResolvedValue(true) }
    await vm.handleSubmit()
    await flushPromises()

    expect(store.token).toBe('a.b.c')
    expect(store.user).toEqual(mockResponse.user)

    vi.advanceTimersByTime(1500)
    expect(mockPush).toHaveBeenCalledWith('/login')
  })

  it('shows error message on ApiError', async () => {
    const { api } = await import('@/utils/request')
    vi.mocked(api.post).mockRejectedValueOnce(new ApiError('用户名已存在', 409))
    const errorSpy = vi.spyOn(message, 'error').mockImplementation(() => ({}) as never)

    await mountPage()
    const vm = getSetupState(wrapper)
    vm.formState.username = 'existing'
    vm.formState.email = 'e@test.com'
    vm.formState.password = 'password123'
    vm.formState.confirmPassword = 'password123'
    vm.formRef = { validate: vi.fn().mockResolvedValue(true) }
    await vm.handleSubmit()
    await flushPromises()

    expect(errorSpy).toHaveBeenCalledWith('用户名已存在')
    errorSpy.mockRestore()
  })

  it('shows network error message on generic error', async () => {
    const { api } = await import('@/utils/request')
    vi.mocked(api.post).mockRejectedValueOnce(new Error('Network failure'))
    const errorSpy = vi.spyOn(message, 'error').mockImplementation(() => ({}) as never)

    await mountPage()
    const vm = getSetupState(wrapper)
    vm.formState.username = 'user'
    vm.formState.email = 'u@test.com'
    vm.formState.password = 'password123'
    vm.formState.confirmPassword = 'password123'
    vm.formRef = { validate: vi.fn().mockResolvedValue(true) }
    await vm.handleSubmit()
    await flushPromises()

    expect(errorSpy).toHaveBeenCalledWith('注册失败，请检查网络连接')
    errorSpy.mockRestore()
  })

  it('navigates to login page', async () => {
    await mountPage()
    const vm = getSetupState(wrapper)
    vm.goToLogin()

    expect(mockPush).toHaveBeenCalledWith('/login')
  })
})
