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 ActionsService {

  constructor(private _http: HttpClient) {}

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

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

  getUserTotalActions(id: string, idUser: string, line: number){
    return this.getQuery(`get-user-total-actions/${id}/${idUser}/${line}`).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
      })
    });
  }
}
