import { ComponentFixture, TestBed } from '@angular/core/testing';
import { MatDialogRef, MAT_DIALOG_DATA } from '@angular/material/dialog';
import { FormsModule } from '@angular/forms';

import { ModalExecuteComponent } from './modal-execute.component';

describe('ModalExecuteComponent', () => {
  let component: ModalExecuteComponent;
  let fixture: ComponentFixture<ModalExecuteComponent>;
  let mockDialogRef: jasmine.SpyObj<MatDialogRef<ModalExecuteComponent>>;

  const mockData = {
    records: [
      { id: 1, name: 'Test Record 1' },
      { id: 2, name: 'Test Record 2' }
    ],
    count: 2,
    moduleName: 'testModule',
    message: 'Voulez-vous exécuter cette opération ?',
    requiresConfirmation: true
  };

  beforeEach(async () => {
    mockDialogRef = jasmine.createSpyObj('MatDialogRef', ['close']);

    await TestBed.configureTestingModule({
      declarations: [ModalExecuteComponent],
      imports: [FormsModule],
      providers: [
        { provide: MatDialogRef, useValue: mockDialogRef },
        { provide: MAT_DIALOG_DATA, useValue: mockData }
      ]
    }).compileComponents();

    fixture = TestBed.createComponent(ModalExecuteComponent);
    component = fixture.componentInstance;
    fixture.detectChanges();
  });

  it('should create', () => {
    expect(component).toBeTruthy();
  });

  it('should initialize with default values', () => {
    expect(component.confirmationChecked).toBeFalse();
    expect(component.data.requiresConfirmation).toBe(true);
  });

  it('should close dialog with cancelled action on cancel', () => {
    component.onCancel();
    
    expect(mockDialogRef.close).toHaveBeenCalledWith({
      confirmed: false,
      action: 'cancelled'
    });
  });

  it('should not confirm if confirmation is required but not checked', () => {
    component.data.requiresConfirmation = true;
    component.confirmationChecked = false;
    
    component.onConfirm();
    
    expect(mockDialogRef.close).not.toHaveBeenCalled();
  });

  it('should confirm execution when confirmation is checked', () => {
    component.data.requiresConfirmation = true;
    component.confirmationChecked = true;
    
    component.onConfirm();
    
    expect(mockDialogRef.close).toHaveBeenCalledWith({
      confirmed: true,
      action: 'execute',
      records: mockData.records,
      moduleName: mockData.moduleName
    });
  });

  it('should confirm execution when confirmation is not required', () => {
    component.data.requiresConfirmation = false;
    
    component.onConfirm();
    
    expect(mockDialogRef.close).toHaveBeenCalledWith({
      confirmed: true,
      action: 'execute',
      records: mockData.records,
      moduleName: mockData.moduleName
    });
  });
}); 