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

const getMock = vi.fn()

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

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

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

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

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

  it('lists operation logs with filters', async () => {
    getMock.mockResolvedValue({
      list: [{ id: 2, operation_name: 'update_user', method: 'PUT' }],
      total: 1,
      page: 2,
      size: 10,
    })

    const { listOperationLogs } = await import('./operation-logs')
    const result = await listOperationLogs({ page: 2, page_size: 10, method: 'PUT' })

    expect(getMock).toHaveBeenCalledWith('/logs/operation', { page: 2, page_size: 10, method: 'PUT' })
    expect(result).toEqual({
      items: [{ id: 2, operation_name: 'update_user', method: 'PUT' }],
      total: 1,
      page: 2,
      page_size: 10,
    })
  })

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

    const { listMyOperationLogs } = await import('./operation-logs')
    const result = await listMyOperationLogs({ page: 1, page_size: 10 })

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