import { ComponentFixture, TestBed } from '@angular/core/testing';
import { FormsModule } from '@angular/forms';
import { ClarityModule } from '@clr/angular';
import { PasswordGeneratorComponent } from './password-generator.component';

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

  beforeEach(async () => {
    await TestBed.configureTestingModule({
      declarations: [ PasswordGeneratorComponent ],
      imports: [ FormsModule, ClarityModule ]
    })
    .compileComponents();
  });

  beforeEach(() => {
    fixture = TestBed.createComponent(PasswordGeneratorComponent);
    component = fixture.componentInstance;
    fixture.detectChanges();
  });

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

  it('should set default values', () => {
    expect(component.passwordLength).toBe(12);
    expect(component.includeUppercase).toBeTrue();
    expect(component.includeLowercase).toBeTrue();
    expect(component.includeNumbers).toBeTrue();
    expect(component.includeSymbols).toBeTrue();
    expect(component.excludeCharacters).toBe('');
    expect(component.generatedPassword).toBe('');
    expect(component.errorMessage).toBe('');
  });

  it('should generate a placeholder password with valid input', () => {
    component.passwordLength = 10;
    component.includeUppercase = true;
    component.includeLowercase = true;
    component.includeNumbers = true;
    component.includeSymbols = true;
    component.excludeCharacters = '';
    component.generatePassword();
    expect(component.generatedPassword).toBe('GeneratedPasswordPlaceholder');
    expect(component.errorMessage).toBe('');
  });

  it('should show error for invalid password length (too short)', () => {
    component.passwordLength = 5;
    component.generatePassword();
    expect(component.generatedPassword).toBe('');
    expect(component.errorMessage).toBe('Password length must be between 8 and 128 characters.');
  });

  it('should show error for invalid password length (too long)', () => {
    component.passwordLength = 150;
    component.generatePassword();
    expect(component.generatedPassword).toBe('');
    expect(component.errorMessage).toBe('Password length must be between 8 and 128 characters.');
  });

  it('should show error if no character type is selected', () => {
    component.passwordLength = 12;
    component.includeUppercase = false;
    component.includeLowercase = false;
    component.includeNumbers = false;
    component.includeSymbols = false;
    component.generatePassword();
    expect(component.generatedPassword).toBe('');
    expect(component.errorMessage).toBe('At least one character type (uppercase, lowercase, numbers, symbols) must be selected.');
  });

  it('should copy generated password to clipboard', () => {
    const writeTextSpy = spyOn(navigator.clipboard, 'writeText').and.returnValue(Promise.resolve());
    spyOn(window, 'alert'); // Mock alert

    component.generatedPassword = 'testPassword';
    component.copyToClipboard();

    expect(writeTextSpy).toHaveBeenCalledWith('testPassword');
    expect(window.alert).toHaveBeenCalledWith('Password copied to clipboard!');
  });

  it('should not copy to clipboard if no password is generated', () => {
    const writeTextSpy = spyOn(navigator.clipboard, 'writeText');
    spyOn(window, 'alert');

    component.generatedPassword = '';
    component.copyToClipboard();

    expect(writeTextSpy).not.toHaveBeenCalled();
    expect(window.alert).not.toHaveBeenCalled();
  });
});
