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

const getMock = vi.fn()
const postMock = vi.fn()
const putMock = vi.fn()
const delMock = vi.fn()

vi.mock('@/lib/http/client', () => ({
  get: getMock,
  post: postMock,
  put: putMock,
  del: delMock,
}))

describe('roles service', () => {
  beforeEach(() => {
    getMock.mockReset()
    postMock.mockReset()
    putMock.mockReset()
    delMock.mockReset()
  })

  it('lists roles with pagination', async () => {
    getMock.mockResolvedValue({
      items: [
        { id: 1, name: '管理员', code: 'admin' },
        { id: 2, name: '用户', code: 'user' },
      ],
      total: 2,
      page: 1,
      page_size: 20,
    })

    const { listRoles } = await import('./roles')
    const result = await listRoles({ page: 1, page_size: 20 })

    expect(getMock).toHaveBeenCalledWith('/roles', { page: 1, page_size: 20 })
    expect(result).toEqual({
      items: [
        { id: 1, name: '管理员', code: 'admin' },
        { id: 2, name: '用户', code: 'user' },
      ],
      total: 2,
      page: 1,
      page_size: 20,
    })
  })

  it('gets a single role', async () => {
    getMock.mockResolvedValue({ id: 3, name: '审计员', code: 'auditor' })

    const { getRole } = await import('./roles')
    const result = await getRole(3)

    expect(getMock).toHaveBeenCalledWith('/roles/3')
    expect(result).toEqual({ id: 3, name: '审计员', code: 'auditor' })
  })

  it('creates a role', async () => {
    const roleData = { name: '新角色', code: 'new_role' }
    const created = { id: 10, ...roleData }
    postMock.mockResolvedValue(created)

    const { createRole } = await import('./roles')
    const result = await createRole(roleData)

    expect(postMock).toHaveBeenCalledWith('/roles', roleData)
    expect(result).toEqual(created)
  })

  it('updates a role', async () => {
    const updateData = { name: '更新的角色', description: '新描述' }
    putMock.mockResolvedValue({ id: 5, ...updateData })

    const { updateRole } = await import('./roles')
    const result = await updateRole(5, updateData)

    expect(putMock).toHaveBeenCalledWith('/roles/5', updateData)
    expect(result).toEqual({ id: 5, ...updateData })
  })

  it('deletes a role', async () => {
    delMock.mockResolvedValue(undefined)

    const { deleteRole } = await import('./roles')
    await deleteRole(7)

    expect(delMock).toHaveBeenCalledWith('/roles/7')
  })

  it('updates role status', async () => {
    putMock.mockResolvedValue(undefined)

    const { updateRoleStatus } = await import('./roles')
    await updateRoleStatus(4, 0)

    expect(putMock).toHaveBeenCalledWith('/roles/4/status', { status: 0 })
  })

  it('gets role permissions', async () => {
    getMock.mockResolvedValue([
      { id: 1, name: 'view' },
      { id: 2, name: 'edit' },
    ])

    const { getRolePermissions } = await import('./roles')
    const result = await getRolePermissions(3)

    expect(getMock).toHaveBeenCalledWith('/roles/3/permissions')
    expect(result).toEqual([1, 2])
  })

  it('assigns permissions to a role', async () => {
    putMock.mockResolvedValue(undefined)

    const { assignRolePermissions } = await import('./roles')
    await assignRolePermissions(3, [1, 2, 3])

    expect(putMock).toHaveBeenCalledWith('/roles/3/permissions', { permission_ids: [1, 2, 3] })
  })
})
