import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { map } from 'rxjs/operators';
import { Observable } from 'rxjs';
import { CONSUMER_PERMISSIONS_API_ROUTES } from '../constants';
import { IPermissionRawResponse, IPermissionRequest, IPermissionResponse } from '../models';

@Injectable({ providedIn: 'root' })
export class PermissionsService {
  constructor(private http: HttpClient) {}

  private apiRoutes = CONSUMER_PERMISSIONS_API_ROUTES;

  getAll(accountId: string): Observable<IPermissionResponse> {
    return this.http.get<IPermissionRawResponse>(this.apiRoutes.list(accountId)).pipe(
      map((res) => ({
        poses: res.poses ?? [],
        complexes: res.complexes ?? [],
        business_activities: res.business_activities ?? res.businessActivities ?? [],
      })),
    );
  }

  update(accountId: string, data: IPermissionRequest): Observable<IPermissionResponse> {
    return this.http.put<IPermissionResponse>(this.apiRoutes.list(accountId), data);
  }
}
