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

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

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

describe('account binding service', () => {
  beforeEach(() => {
    postMock.mockReset()
    delMock.mockReset()
    postMock.mockResolvedValue(undefined)
    delMock.mockResolvedValue(undefined)
  })

  it('sends email bind code through the protected endpoint', async () => {
    const { sendEmailBindCode } = await import('./account-bindings')

    await sendEmailBindCode({ email: 'bind@example.com' })

    expect(postMock).toHaveBeenCalledWith('/users/me/bind-email/code', {
      email: 'bind@example.com',
    })
  })

  it('binds email with verification payload', async () => {
    const { bindEmail } = await import('./account-bindings')

    await bindEmail({
      email: 'bind@example.com',
      code: '123456',
      current_password: 'SecurePass123',
    })

    expect(postMock).toHaveBeenCalledWith('/users/me/bind-email', {
      email: 'bind@example.com',
      code: '123456',
      current_password: 'SecurePass123',
    })
  })

  it('unbinds phone with optional sensitive verification', async () => {
    const { unbindPhone } = await import('./account-bindings')

    await unbindPhone({ totp_code: '654321' })

    expect(delMock).toHaveBeenCalledWith('/users/me/bind-phone', {
      body: { totp_code: '654321' },
    })
  })
})
