import { ComponentFixture, TestBed } from '@angular/core/testing';
import { HttpClientTestingModule } from '@angular/common/http/testing';
import { ReactiveFormsModule } from '@angular/forms';
import { MatDialogModule, MatDialogRef, MAT_DIALOG_DATA } from '@angular/material/dialog';
import { MatSnackBarModule } from '@angular/material/snack-bar';
import { ToastrModule } from 'ngx-toastr';
import { TranslateModule } from '@ngx-translate/core';
import { MatProgressSpinnerModule } from '@angular/material/progress-spinner';
import { ManageClientComponent } from './manage-client.component';
import { DataService } from '../../../services/data.service';
import { ConfigService } from '@services/config.service';

describe('ManageClientComponent', () => {
  let component: ManageClientComponent;
  let fixture: ComponentFixture<ManageClientComponent>;

  beforeEach(async () => {
    const mockConfigService = {
      apiUrl: 'http://mock-api-url',
      mercureUrl: 'http://mock-mercure-url'
    };

    await TestBed.configureTestingModule({
      declarations: [ManageClientComponent],
      imports: [
        HttpClientTestingModule,
        ReactiveFormsModule,
        MatDialogModule,
        MatSnackBarModule,
        ToastrModule.forRoot(),
        TranslateModule.forRoot(),
        MatProgressSpinnerModule
      ],
      providers: [
        { provide: MatDialogRef, useValue: {} },
        { provide: MAT_DIALOG_DATA, useValue: { uuid: '123', organizationalUnit: { '@id': '/units/1' } } },
        { provide: ConfigService, useValue: mockConfigService },
        DataService
      ]
    })
    .compileComponents();

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

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

  it('should initialize form', () => {
    expect(component.clientForm).toBeDefined();
    expect(component.clientForm.controls['name']).toBeDefined();
    expect(component.clientForm.controls['mac']).toBeDefined();
    expect(component.clientForm.controls['ip']).toBeDefined();
  });

  it('should set dialog title for edit mode', () => {
    component.isEditMode = true;
    component.data = { uuid: '123', organizationalUnit: { '@id': '/units/1' } };
    component.ngOnInit();
    expect(component.dialogTitle).toBe('editClientDialogTitle');
  });

  it('should call onSubmit when form is valid', () => {
    spyOn(component, 'onSubmit');
    component.clientForm.controls['name'].setValue('Test Client');
    component.clientForm.controls['mac'].setValue('00:11:22:33:44:55');
    component.clientForm.controls['ip'].setValue('192.168.1.1');
    fixture.detectChanges();
    const button = fixture.debugElement.nativeElement.querySelector('.submit-button');
    button.click();
    expect(component.onSubmit).toHaveBeenCalled();
  });
});