import { createComponentFactory, Spectator } from '@ngneat/spectator/jest';
import {
  FeedbackErrorDialogComponent,
  FeedbackErrorDialogData,
} from './feedback-error-dialog.component';
import { DialogRef, DIALOG_DATA } from '@angular/cdk/dialog';
import { NgIcon } from '@ng-icons/core';
import { DialogComponent } from '../dialog.component';

describe('FeedbackErrorDialogComponent', () => {
  let spectator: Spectator<FeedbackErrorDialogComponent>;
  let currentDialogData: FeedbackErrorDialogData;

  const createComponent = createComponentFactory({
    component: FeedbackErrorDialogComponent,
    imports: [NgIcon],
    providers: [
      {
        provide: DialogRef,
        useValue: { close: jest.fn() },
      },
      {
        provide: DIALOG_DATA,
        useFactory: () => currentDialogData,
      },
      {
        provide: DialogComponent,
        useValue: {},
      },
    ],
  });

  it('should create', () => {
    // Arrange
    currentDialogData = { errorMessage: 'Test error' };

    // Act
    spectator = createComponent();

    // Assert
    expect(spectator.component).toBeTruthy();
  });

  describe('error message display', () => {
    it('should display error message from errorMessage property', () => {
      // Arrange
      currentDialogData = {
        errorMessage: 'Something went wrong',
      };
      spectator = createComponent();

      // Act
      const messageElement = spectator.query('[data-what="error-message"]');

      // Assert
      expect(messageElement).toHaveText('Something went wrong');
    });

    it('should display error message from Error object', () => {
      // Arrange
      const testError = new Error('Test error message');
      currentDialogData = { error: testError };
      spectator = createComponent();

      // Act
      const messageElement = spectator.query('[data-what="error-message"]');

      // Assert
      expect(messageElement).toHaveText('Test error message');
    });

    it('should display fallback message for unknown error type', () => {
      // Arrange
      currentDialogData = { error: 'invalid error' };
      spectator = createComponent();

      // Act
      const messageElement = spectator.query('[data-what="error-message"]');

      // Assert
      expect(messageElement).toHaveText(
        'Ein unbekannter Fehler ist aufgetreten',
      );
    });
  });

  describe('icon rendering', () => {
    it('should render the close icon with correct attributes', () => {
      // Arrange
      currentDialogData = { errorMessage: 'Test error' };
      spectator = createComponent();

      // Act
      const iconElement = spectator.query('ng-icon');

      // Assert
      expect(iconElement).toBeTruthy();
      expect(iconElement).toHaveAttribute('name', 'isaActionClose');
      expect(iconElement).toHaveAttribute('size', '1.5rem');
    });
  });
});
