import { describe, it, expect, vi } from 'vitest';
import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { DeactivateWarningDialog, DeactivateWarning } from './DeactivateWarningDialog';

const mockWarning: DeactivateWarning = {
  slug: 'mailchimp',
  packageName: 'Mailchimp',
  workflows: [
    { id: 1, name: 'Welcome Email' },
    { id: 2, name: 'Newsletter Signup' },
  ],
  message: 'Deactivating this package will deactivate 2 workflows that use its actions.',
};

describe('DeactivateWarningDialog', () => {
  it('renders affected workflow names when warning is provided', () => {
    render(
      <DeactivateWarningDialog
        warning={mockWarning}
        onConfirm={vi.fn()}
        onCancel={vi.fn()}
      />,
    );

    expect(screen.getByText('Welcome Email')).toBeInTheDocument();
    expect(screen.getByText('Newsletter Signup')).toBeInTheDocument();
  });

  it('shows warning message text', () => {
    render(
      <DeactivateWarningDialog
        warning={mockWarning}
        onConfirm={vi.fn()}
        onCancel={vi.fn()}
      />,
    );

    expect(
      screen.getByText('Deactivating this package will deactivate 2 workflows that use its actions.'),
    ).toBeInTheDocument();
  });

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

    render(
      <DeactivateWarningDialog
        warning={mockWarning}
        onConfirm={onConfirm}
        onCancel={vi.fn()}
      />,
    );

    await user.click(screen.getByRole('button', { name: /deactivate/i }));
    expect(onConfirm).toHaveBeenCalledOnce();
  });

  it('calls onCancel when Cancel is clicked', async () => {
    const user = userEvent.setup();
    const onCancel = vi.fn();

    render(
      <DeactivateWarningDialog
        warning={mockWarning}
        onConfirm={vi.fn()}
        onCancel={onCancel}
      />,
    );

    await user.click(screen.getByRole('button', { name: /cancel/i }));
    expect(onCancel).toHaveBeenCalledOnce();
  });

  it('does not render dialog content when warning is null', () => {
    render(
      <DeactivateWarningDialog
        warning={null}
        onConfirm={vi.fn()}
        onCancel={vi.fn()}
      />,
    );

    expect(screen.queryByText('Affected workflows:')).not.toBeInTheDocument();
    expect(screen.queryByRole('button', { name: /deactivate/i })).not.toBeInTheDocument();
  });
});
