import { inject, Injectable } from '@angular/core';
import { StatsService } from '@iqser/common-ui';
import { DOSSIER_ID, DossierStats, IDossierStats } from '@red/domain';
import { Observable, of } from 'rxjs';
import { UserService } from '@users/user.service';
import { NGXLogger } from 'ngx-logger';
import { tap } from 'rxjs/operators';

@Injectable({
    providedIn: 'root',
})
export class DossierStatsService extends StatsService<DossierStats, IDossierStats> {
    protected readonly _primaryKey = DOSSIER_ID;
    protected readonly _entityClass = DossierStats;
    protected readonly _defaultModelPath = 'dossier-stats';
    readonly #userService = inject(UserService);
    readonly #logger = inject(NGXLogger);

    getFor(ids: string[]): Observable<DossierStats[]> {
        const isUserAdminOnly = this.#userService.currentUser.roles.length === 1 && this.#userService.currentUser.isUserAdmin;
        if (isUserAdminOnly) {
            return of([]);
        }

        return super.getFor(ids).pipe(tap(stats => this.#logger.info('[STATS] Loaded', stats)));
    }
}
