import { Inject, Injectable } from "@angular/core";
import { UserRep } from "../../models/user-management.api.models";
import { ApiService } from "./user-management.api.service";
import { HttpClient, HttpParams } from "@angular/common/http";
import { Observable } from "rxjs";
import { UrlService } from "./url.service";

@Injectable({
  providedIn: "root"
})
export class UserRepService extends ApiService<UserRep> {
  constructor(http: HttpClient, urlService: UrlService) {
    super(http, urlService.apiRoute.userRep)
  }

  override getAll(options?: Options): Observable<UserRep[]> {
    let params = new HttpParams();
    if (options) {
      if (options.withUser) {
        params = params.set('withUser', options.withUser);
      }
      if (options.withRepGroup) {
        params = params.set('withRepGroup', options.withRepGroup);
      }
      if (options.withGroup) {
        params = params.set('withRightGroup', options.withGroup);
      }
      if (options.withRepUser) {
        params = params.set('withRepUser', options.withRepUser);
      }
      if (options.userId) {
        params = params.set('userId', options.userId)
      }
      if (options.groupId) {
        params = params.set('groupId', options.groupId)
      }
    }

    return this.http.get<UserRep[]>(`${this.baseUrl}`, { params: params, withCredentials: true });
  }
}

interface Options {
  withUser: boolean;
  withRepGroup: boolean;
  withGroup: boolean;
  withRepUser: boolean;
  userId?: number;
  groupId?: number;
}