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

import {render, screen, userEvent} from '../../test-utils'

import {AlertDialogAtom} from './alert-dialog'

describe('AlertDialogAtom', () => {
  const defaultProps = {
    title: 'Confirm Action',
    description: 'Are you sure you want to proceed?',
    cancelButtonText: 'Cancel',
    confirmationButtonText: 'Confirm',
    open: false,
    onOpenChange: vi.fn(),
    onConfirmationAction: vi.fn(),
  }

  it('renders trigger element', () => {
    render(
      <AlertDialogAtom {...defaultProps}>
        <button type="button">Open Dialog</button>
      </AlertDialogAtom>
    )

    expect(
      screen.getByRole('button', {name: /open dialog/i})
    ).toBeInTheDocument()
  })

  it('shows dialog content when open', () => {
    render(
      <AlertDialogAtom {...defaultProps} open>
        <button type="button">Open Dialog</button>
      </AlertDialogAtom>
    )

    expect(screen.getByText(defaultProps.title)).toBeInTheDocument()
    expect(screen.getByText(defaultProps.description)).toBeInTheDocument()
    expect(
      screen.getByRole('button', {name: defaultProps.cancelButtonText})
    ).toBeInTheDocument()
    expect(
      screen.getByRole('button', {name: defaultProps.confirmationButtonText})
    ).toBeInTheDocument()
  })

  it('calls onConfirmationAction when confirm button is clicked', async () => {
    const user = userEvent.setup()
    const onConfirmationAction = vi.fn()

    render(
      <AlertDialogAtom
        {...defaultProps}
        open
        onConfirmationAction={onConfirmationAction}
      >
        <button type="button">Open Dialog</button>
      </AlertDialogAtom>
    )

    await user.click(
      screen.getByRole('button', {name: defaultProps.confirmationButtonText})
    )

    expect(onConfirmationAction).toHaveBeenCalledTimes(1)
  })
})
