import { apiTemp, apiUriQA } from './../../environments/environment';
import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { map } from 'rxjs';

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

  constructor(private http: HttpClient) {}

  getUsers(id: string, line: number) {
    return this.getQuery(`get-users/${id}/${line}`).pipe(map((data: any) => data));
  }

  getInitialPermissions(id: string, line: number){
    return this.getQuery(`get-initial-permissions/${id}/${line}`).pipe(map((data: any) => data))
  }

  getUser(idUser: string, id: string, line: number) {
    return this.getQuery(`get-user/${idUser}/${id}/${line}`).pipe(map((data: any) => data));
  }

  getProfiles(id: string, line: number) {
    return this.getQuery(`get-profiles/${id}/${line}`).pipe(map((data: any) => data));
  }

  getProfile(idProfile: string, id: string, line: number){
    return this.getQuery(`get-profile/${idProfile}/${id}/${line}`).pipe(map((data: any) => data))
  }

  updateProfile(info: any){
    return this.postQuery("modify-profile", info).pipe(map((data: any) => data))
  }

  updateUser(userInfo: any){
    return this.postQuery("modify-user", userInfo).pipe(map((data: any) => data))
  }

  newUser(userInfo: any) {
    return this.postQuery('create-user', userInfo).pipe(
      map((data: any) => data)
    );
  }

  newProfile(profileInfo: any){
    return this.postQuery("create-profile", profileInfo).pipe(map((data: any) => data))
  }

  blockUser(userInfo: any) {
    return this.postQuery('block-user', userInfo).pipe(
      map((data: any) => data)
    );
  }

  updatePwd(userInfo: any) {
    return this.postQuery('modify-pass', userInfo).pipe(
      map((data: any) => data)
    );
  }

  deleteUser(userInfo: any) {
    return this.postQuery('delete-user', userInfo).pipe(
      map((data: any) => data)
    );
  }

  deleteProfile(profileInfo: any){
    return this.postQuery("delete-profile", profileInfo).pipe(map((data: any) => data))
  }

  getQuery(query: string){
    const URL = `${apiTemp}${query}`;
    const token = `Bearer ${localStorage.getItem('token')!}`;
    return this.http.get(URL, {
      headers: new HttpHeaders({
        Authorization: token
      })
    });
  }

  postQuery(query: string, body: any){
    const URL = `${apiTemp}${query}`;
    const token = `Bearer ${localStorage.getItem('token')!}`;
    return this.http.post(URL, body, {
      headers: new HttpHeaders({
        Authorization: token
      })
    });
  }
}
