import React from 'react';
import { describe, it, expect, vi } from 'vitest';
import { render, screen, fireEvent } from '@testing-library/react';
import { ConfirmDialog } from '@/components/ui/ConfirmDialog';

describe('ConfirmDialog', () => {
  it('renders nothing when open=false', () => {
    render(<ConfirmDialog open={false} message="Wirklich löschen?" onConfirm={() => {}} onCancel={() => {}} />);
    expect(screen.queryByText('Wirklich löschen?')).not.toBeInTheDocument();
  });

  it('renders message when open=true', () => {
    render(<ConfirmDialog open={true} message="Wirklich löschen?" onConfirm={() => {}} onCancel={() => {}} />);
    expect(screen.getByText('Wirklich löschen?')).toBeInTheDocument();
  });

  it('calls onConfirm when confirm button clicked', () => {
    const onConfirm = vi.fn();
    render(<ConfirmDialog open={true} message="Löschen?" onConfirm={onConfirm} onCancel={() => {}} />);
    fireEvent.click(screen.getByRole('button', { name: 'Bestätigen' }));
    expect(onConfirm).toHaveBeenCalledTimes(1);
  });

  it('calls onCancel when cancel button clicked', () => {
    const onCancel = vi.fn();
    render(<ConfirmDialog open={true} message="Löschen?" onConfirm={() => {}} onCancel={onCancel} />);
    fireEvent.click(screen.getByRole('button', { name: 'Abbrechen' }));
    expect(onCancel).toHaveBeenCalledTimes(1);
  });

  it('renders default title', () => {
    render(<ConfirmDialog open={true} message="Test" onConfirm={() => {}} onCancel={() => {}} />);
    expect(screen.getByText('Bestätigung erforderlich')).toBeInTheDocument();
  });

  it('renders custom title', () => {
    render(<ConfirmDialog open={true} title="Löschen bestätigen" message="Test" onConfirm={() => {}} onCancel={() => {}} />);
    expect(screen.getByText('Löschen bestätigen')).toBeInTheDocument();
  });

  it('uses danger variant button when variant=danger', () => {
    render(<ConfirmDialog open={true} variant="danger" message="Test" onConfirm={() => {}} onCancel={() => {}} />);
    const confirmBtn = screen.getByRole('button', { name: 'Bestätigen' });
    expect(confirmBtn.className).toContain('bg-danger-600');
  });
});
