/**
 * Scan Timeline — second-run modal (Section 3.2).
 *
 * The choice is only offered when there is a graph to lose, it defaults to the
 * non-destructive option, and the chosen mode is what reaches the caller.
 */
import { describe, test, expect, vi, beforeEach, afterEach } from 'vitest'
import { render, screen, fireEvent, cleanup } from '@testing-library/react'
import { ReconConfirmModal } from './ReconConfirmModal'

const baseProps = {
  isOpen: true,
  onClose: vi.fn(),
  onConfirm: vi.fn(),
  projectName: 'Acme',
  targetDomain: 'acme.tld',
  isLoading: false,
}

const withData = { totalNodes: 120, nodesByType: { IP: 40, Port: 80 } }

beforeEach(() => vi.clearAllMocks())
// The suite has no `globals: true`, so RTL's auto-cleanup does not run and the
// modal's portal would otherwise stack across tests.
afterEach(cleanup)

describe('ReconConfirmModal — scan mode choice', () => {
  test('no existing data → no choice, confirms with the default mode', () => {
    render(<ReconConfirmModal {...baseProps} stats={null} />)
    expect(screen.queryByRole('radiogroup')).toBeNull()
    fireEvent.click(screen.getByText('Start Recon'))
    expect(baseProps.onConfirm).toHaveBeenCalledWith('new')
  })

  test('existing data → both options offered, "new version" preselected', () => {
    render(<ReconConfirmModal {...baseProps} stats={withData} />)
    const radios = screen.getAllByRole('radio') as HTMLInputElement[]
    expect(radios).toHaveLength(2)
    expect(radios[0].value).toBe('new')
    expect(radios[0].checked).toBe(true)
    expect(radios[1].value).toBe('overwrite')
    expect(radios[1].checked).toBe(false)
    expect(screen.getByText('Save Version & Start')).toBeTruthy()
  })

  test('choosing overwrite confirms with overwrite and relabels the action', () => {
    render(<ReconConfirmModal {...baseProps} stats={withData} />)
    fireEvent.click(screen.getAllByRole('radio')[1])
    expect(screen.getByText('Discard & Start')).toBeTruthy()
    fireEvent.click(screen.getByText('Discard & Start'))
    expect(baseProps.onConfirm).toHaveBeenCalledWith('overwrite')
  })

  test('shows the label the current graph would be saved as', () => {
    render(<ReconConfirmModal {...baseProps} stats={withData} currentVersionLabel="Scan 2 - 2026-07-01 10:00 UTC" />)
    expect(screen.getByText(/Scan 2 - 2026-07-01 10:00 UTC/)).toBeTruthy()
  })

  test('reopening resets a previously chosen overwrite back to the safe default', () => {
    const { rerender } = render(<ReconConfirmModal {...baseProps} stats={withData} />)
    fireEvent.click(screen.getAllByRole('radio')[1])
    expect((screen.getAllByRole('radio')[1] as HTMLInputElement).checked).toBe(true)
    rerender(<ReconConfirmModal {...baseProps} isOpen={false} stats={withData} />)
    rerender(<ReconConfirmModal {...baseProps} isOpen stats={withData} />)
    expect((screen.getAllByRole('radio')[0] as HTMLInputElement).checked).toBe(true)
  })

  test('while starting, the options are disabled', () => {
    render(<ReconConfirmModal {...baseProps} stats={withData} isLoading />)
    for (const r of screen.getAllByRole('radio') as HTMLInputElement[]) {
      expect(r.disabled).toBe(true)
    }
  })
})
