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

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

  constructor(private _http: HttpClient) {}

  getUsersConnections(idUser: string, line: number){
    return this.getQuery(`get-last-connections/${idUser}/${line}`).pipe(map((data: any) => data))
  }

  getUserConnections(id: string, idUser: string, line: number){
    return this.getQuery(`get-user-connections/${id}/${idUser}/${line}`).pipe(map((data: any) => data))
  }
  
  setLastConnection(body: any){
    return this.postQuery("set-last-connection", body).pipe(map((data: any) => data));
  }

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

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