import { fireEvent, render, screen } from '@testing-library/react'
import { describe, expect, it, vi } from 'vitest'
import { CAPACITY_ACCOUNTS, LIMITED_CODEX } from './fixtures/capacity'
import { RateLimitDialog } from './RateLimitDialog'

describe('RateLimitDialog', () => {
  it('renders a healthy wrapper as read-only', () => {
    render(
      <RateLimitDialog
        limit={null}
        wrapper="cursor"
        accounts={CAPACITY_ACCOUNTS}
        open
        onClose={vi.fn()}
        onResolve={vi.fn()}
      />,
    )

    expect(screen.getByText('No limit — account chain healthy')).toBeInTheDocument()
    expect(screen.queryByRole('radio')).not.toBeInTheDocument()
  })

  it('renders limited scope and resolves wait, retry, and switch payloads', async () => {
    const onResolve = vi.fn()
    render(
      <RateLimitDialog
        limit={LIMITED_CODEX}
        wrapper="codex"
        accounts={CAPACITY_ACCOUNTS}
        open
        onClose={vi.fn()}
        onResolve={onResolve}
      />,
    )

    expect(screen.getByText('What happened')).toBeInTheDocument()
    expect(screen.getByText('Affects coder, fixer · parked t3, t5')).toBeInTheDocument()
    expect(screen.getByRole('radio', { name: /Wait — safe/ })).toBeChecked()
    fireEvent.click(screen.getByRole('button', { name: 'Apply resolution' }))
    expect(onResolve).toHaveBeenLastCalledWith({ kind: 'wait' })

    fireEvent.click(screen.getByRole('radio', { name: /Retry now/ }))
    fireEvent.click(screen.getByRole('button', { name: 'Apply resolution' }))
    expect(onResolve).toHaveBeenLastCalledWith({ kind: 'retry' })

    fireEvent.click(screen.getByRole('radio', { name: /Switch account/ }))
    fireEvent.click(screen.getByRole('combobox', { name: 'codex account chain' }))
    fireEvent.click(await screen.findByRole('option', { name: /^team/ }))
    fireEvent.click(screen.getByRole('button', { name: 'Apply resolution' }))
    expect(onResolve).toHaveBeenLastCalledWith({ kind: 'switch', account: 'team' })
  })
})
