import { render, screen, fireEvent } from '@testing-library/react';
import { vi, describe, it, expect } from 'vitest';
import { ConfirmWithJustificationDialog } from '../confirm-with-justification-dialog';

describe('ConfirmWithJustificationDialog', () => {
  it('confirm button is disabled until justification is at least 20 chars', () => {
    const onConfirm = vi.fn();
    render(
      <ConfirmWithJustificationDialog
        open
        title="t"
        description="d"
        confirmLabel="Confirm"
        onConfirm={onConfirm}
        onCancel={() => {}}
      />,
    );
    const button = screen.getByRole('button', { name: 'Confirm' });
    expect(button).toBeDisabled();

    fireEvent.change(screen.getByLabelText('Justification'), {
      target: { value: 'short' },
    });
    expect(button).toBeDisabled();

    fireEvent.change(screen.getByLabelText('Justification'), {
      target: { value: 'this is a sufficiently long justification' },
    });
    expect(button).not.toBeDisabled();
    fireEvent.click(button);
    expect(onConfirm).toHaveBeenCalledWith('this is a sufficiently long justification');
  });

  it('cancel button calls onCancel', () => {
    const onCancel = vi.fn();
    render(
      <ConfirmWithJustificationDialog
        open
        title="t"
        description="d"
        confirmLabel="Go"
        onConfirm={() => {}}
        onCancel={onCancel}
      />,
    );
    fireEvent.click(screen.getByRole('button', { name: /Cancel/ }));
    expect(onCancel).toHaveBeenCalled();
  });

  it('renders nothing when open=false', () => {
    const { container } = render(
      <ConfirmWithJustificationDialog
        open={false}
        title="t"
        description="d"
        confirmLabel="x"
        onConfirm={() => {}}
        onCancel={() => {}}
      />,
    );
    expect(container.firstChild).toBeNull();
  });
});
