import { beforeEach, describe, expect, it, vi } from 'vitest'

const getMock = vi.fn()
const downloadMock = vi.fn()

vi.mock('@/lib/http/client', () => ({
  get: getMock,
  download: downloadMock,
}))

describe('login-logs service', () => {
  beforeEach(() => {
    getMock.mockReset()
    downloadMock.mockReset()
  })

  it('lists login logs with pagination', async () => {
    getMock.mockResolvedValue({
      list: [{ id: 1, status: 1, login_type: 1 }],
      total: 1,
      page: 1,
      size: 20,
    })

    const { listLoginLogs } = await import('./login-logs')
    const result = await listLoginLogs({ page: 1, page_size: 20 })

    expect(getMock).toHaveBeenCalledWith('/logs/login', { page: 1, page_size: 20 })
    expect(result).toEqual({
      items: [{ id: 1, status: 1, login_type: 1 }],
      total: 1,
      page: 1,
      page_size: 20,
    })
  })

  it('lists login logs with filters', async () => {
    getMock.mockResolvedValue({
      list: [{ id: 2, status: 0 }],
      total: 1,
      page: 2,
      size: 10,
    })

    const { listLoginLogs } = await import('./login-logs')
    const result = await listLoginLogs({ page: 2, page_size: 10, status: 0 })

    expect(getMock).toHaveBeenCalledWith('/logs/login', { page: 2, page_size: 10, status: 0 })
    expect(result).toEqual({
      items: [{ id: 2, status: 0 }],
      total: 1,
      page: 2,
      page_size: 10,
    })
  })

  it('lists my login logs', async () => {
    getMock.mockResolvedValue({
      list: [{ id: 3, status: 1 }],
      total: 3,
      page: 1,
      size: 5,
    })

    const { listMyLoginLogs } = await import('./login-logs')
    const result = await listMyLoginLogs({ page: 1, page_size: 5 })

    expect(getMock).toHaveBeenCalledWith('/logs/login/me', { page: 1, page_size: 5 })
    expect(result).toEqual({
      items: [{ id: 3, status: 1 }],
      total: 3,
      page: 1,
      page_size: 5,
    })
  })
})
