import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { environment } from 'src/environments/environment';
import { UserProfileModel } from '../models/user.model';
import { map, tap, switchMap, filter, reduce } from "rxjs/operators";

@Injectable({
  providedIn: 'root'
})
export class UserService {

  apiBaseUrl = environment.baseUrl + "/users";
  constructor(
    private http: HttpClient
  ) { }

  getById(id: string) {
    return this.http
      .get<UserProfileModel>(this.apiBaseUrl + "/" + id)
      .pipe(map((e) => new UserProfileModel(e)));
  }

  getLists() {
    return this.http
      .get<UserProfileModel[]>(this.apiBaseUrl)
      .pipe(
        map((e) => e.map((e) => new UserProfileModel(e)))
      );
  }

  save(body: UserProfileModel) {
    return this.http.post<{
      "message": string,
      "user": UserProfileModel
    }>(this.apiBaseUrl, new UserProfileModel(body));
  }

  update(body: UserProfileModel) {
    return this.http.put<{
      "message": string,
      "user": UserProfileModel
    }>(this.apiBaseUrl + "/" + body.user_id, new UserProfileModel(body));
  }

  delete(body: UserProfileModel) {
    return this.http.delete<{
      "message": string,
      "user": UserProfileModel
    }>(this.apiBaseUrl + "/" + body.user_id);
  }

}
