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

vi.mock('./admin.repo', () => ({
  findAdminByEmail: vi.fn(),
  setAdminPasswordReset: vi.fn(),
}))

vi.mock('../../services/notificationService', () => ({
  sendTransactionalEmail: vi.fn().mockResolvedValue(undefined),
}))

import * as repo from './admin.repo'
import { sendTransactionalEmail } from '../../services/notificationService'
import { forgotPassword } from './admin.service'

describe('admin.service forgotPassword', () => {
  const originalAdminUrl = process.env.ADMIN_URL

  beforeEach(() => {
    vi.clearAllMocks()
    process.env.ADMIN_URL = 'http://localhost:3000/admin'
  })

  afterAll(() => {
    process.env.ADMIN_URL = originalAdminUrl
  })

  it('sends the reset email to the canonical stored admin address', async () => {
    vi.mocked(repo.findAdminByEmail).mockResolvedValue({
      id: 'admin_1',
      email: 'admin@rentaldrivego.com',
      firstName: 'Samir',
      isActive: true,
    } as any)

    await forgotPassword('Admin@RentalDriveGo.com')

    expect(repo.setAdminPasswordReset).toHaveBeenCalledWith(
      'admin_1',
      expect.any(String),
      expect.any(Date),
    )
    expect(sendTransactionalEmail).toHaveBeenCalledWith(
      expect.objectContaining({
        to: 'admin@rentaldrivego.com',
      }),
    )
  })
})
