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('permissions service', () => {
  beforeEach(() => {
    getMock.mockReset()
    postMock.mockReset()
    putMock.mockReset()
    delMock.mockReset()
  })

  it('gets permission tree', async () => {
    const mockTree = [
      { id: 1, name: 'dashboard', children: [{ id: 2, name: 'view' }] },
    ]
    getMock.mockResolvedValue(mockTree)

    const { getPermissionTree } = await import('./permissions')
    const result = await getPermissionTree()

    expect(getMock).toHaveBeenCalledWith('/permissions/tree')
    expect(result).toEqual(mockTree)
  })

  it('lists all permissions', async () => {
    const mockPermissions = [
      { id: 1, name: 'view dashboard', code: 'dashboard:view' },
      { id: 2, name: 'edit dashboard', code: 'dashboard:edit' },
    ]
    getMock.mockResolvedValue(mockPermissions)

    const { listPermissions } = await import('./permissions')
    const result = await listPermissions()

    expect(getMock).toHaveBeenCalledWith('/permissions')
    expect(result).toEqual(mockPermissions)
  })

  it('gets a single permission', async () => {
    getMock.mockResolvedValue({ id: 5, name: 'view users', code: 'users:view' })

    const { getPermission } = await import('./permissions')
    const result = await getPermission(5)

    expect(getMock).toHaveBeenCalledWith('/permissions/5')
    expect(result).toEqual({ id: 5, name: 'view users', code: 'users:view' })
  })

  it('creates a permission', async () => {
    const newPermission = { name: 'new permission', code: 'new:code', type: 'button' as const }
    const created = { id: 10, ...newPermission }
    postMock.mockResolvedValue(created)

    const { createPermission } = await import('./permissions')
    const result = await createPermission(newPermission)

    expect(postMock).toHaveBeenCalledWith('/permissions', newPermission)
    expect(result).toEqual(created)
  })

  it('updates a permission', async () => {
    const updateData = { name: 'updated name' }
    putMock.mockResolvedValue({ id: 3, ...updateData })

    const { updatePermission } = await import('./permissions')
    const result = await updatePermission(3, updateData)

    expect(putMock).toHaveBeenCalledWith('/permissions/3', updateData)
    expect(result).toEqual({ id: 3, name: 'updated name' })
  })

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

    const { deletePermission } = await import('./permissions')
    await deletePermission(7)

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

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

    const { updatePermissionStatus } = await import('./permissions')
    await updatePermissionStatus(4, 0)

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