import { inject, Injectable } from '@angular/core';
import { GenericService } from '@iqser/common-ui';
import { SystemPreferences } from '@red/domain';
import { firstValueFrom } from 'rxjs';
import { NGXLogger } from 'ngx-logger';

@Injectable({
    providedIn: 'root',
})
export class SystemPreferencesService extends GenericService<SystemPreferences> {
    values: SystemPreferences;
    protected readonly _defaultModelPath = 'app-config';
    readonly #logger = inject(NGXLogger);

    async loadPreferences() {
        this.values = await firstValueFrom(this.get<SystemPreferences>());
        this.#logger.info('[REDACTION] Loaded config: ', this.values);
        return this.values;
    }

    async loadPreferencesIfNeeded() {
        if (!this.values) {
            await this.loadPreferences();
        }
    }

    async update(value: SystemPreferences) {
        await firstValueFrom(this._post(value));
        return await this.loadPreferences();
    }
}
