import {TestBed} from '@angular/core/testing';
import {firstValueFrom, of} from 'rxjs';
import {DbService} from './db.service';
import {GlobalSettingsService} from './global-settings.service';

describe('GlobalSettingsService', () => {
  let service: GlobalSettingsService;
  let dbServiceSpy: jasmine.SpyObj<DbService>;
  let updateSpy: jasmine.Spy;

  beforeEach(async () => {
    updateSpy = jasmine.createSpy('update').and.resolveTo();
    dbServiceSpy = jasmine.createSpyObj<DbService>('DbService', ['doc$', 'doc']);
    dbServiceSpy.doc$.and.returnValue(of({churchName: 'ICF'}) as never);
    dbServiceSpy.doc.and.returnValue({update: updateSpy} as never);

    await TestBed.configureTestingModule({
      providers: [{provide: DbService, useValue: dbServiceSpy}],
    });

    service = TestBed.inject(GlobalSettingsService);
  });

  it('should be created', () => {
    expect(service).toBeTruthy();
  });

  it('should read the static global settings document once on creation', () => {
    expect(dbServiceSpy.doc$).toHaveBeenCalledTimes(1);
    expect(dbServiceSpy.doc$).toHaveBeenCalledWith('global/static');
  });

  it('should expose the shared settings stream via the getter', async () => {
    await expectAsync(firstValueFrom(service.get$)).toBeResolvedTo({churchName: 'ICF'} as never);
  });

  it('should update the static global settings document', async () => {
    await service.set({churchName: 'New Name'} as never);

    expect(dbServiceSpy.doc).toHaveBeenCalledWith('global/static');
    expect(updateSpy).toHaveBeenCalledWith({churchName: 'New Name'} as never);
  });
});
