import { inject, Injectable } from '@angular/core';
import { HttpClient, HttpResponse } from '@angular/common/http';
import { Observable } from 'rxjs';

import { isPresent } from 'app/core/util/operators';
import { ApplicationConfigService } from 'app/core/config/application-config.service';
import { ResilientService } from 'app/resilient/resilient-base/resilient-base.service';
import { createRequestOption } from 'app/core/request/request-util';
import { ISecurityGroup, NewSecurityGroup } from '../security-group.model';
import { ISecurityGroupPermission } from '../security-group-permission.model';

export type PartialUpdateSecurityGroup = Partial<ISecurityGroup> & Pick<ISecurityGroup, 'id'>;

export type EntityResponseType = HttpResponse<ISecurityGroup>;
export type EntityArrayResponseType = HttpResponse<ISecurityGroup[]>;

@Injectable({ providedIn: 'root' })
export class SecurityGroupService extends ResilientService<ISecurityGroup, NewSecurityGroup> {
  protected applicationConfigService = inject(ApplicationConfigService);

  private resourceUrl = this.applicationConfigService.getEndpointFor('api/security/groups');
  getResourceUrl(): string { return this.resourceUrl; }

  // Get a list of UnitConverters, that applies to fromUnit and toUnit
  queryPermissions(securityGroupId: number): Observable<HttpResponse<ISecurityGroupPermission[]>> {
    return this.http.get<ISecurityGroupPermission[]>(`${this.resourceUrl}/${securityGroupId}/permissions`, { observe: 'response' });
  }  
}
