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

@Injectable({
  providedIn: 'root'
})
export class UsersService {
  private URL: string = environments.baseUrl;
  private getHeaders(): HttpHeaders {
    const token = localStorage.getItem('token') || '';
    return new HttpHeaders({
      'Content-Type': 'application/json',
      'Authorization': `Bearer ${token}`
    });
  }
  constructor(private http: HttpClient) { }

  getStats(){
    return this.http.get(`${this.URL}/count`, { headers: this.getHeaders() })
  }
  
  getAllUsers(){
    return this.http.get(`${this.URL}/usuarioAll`, { headers: this.getHeaders() })
  }

  crearUsuario(data: any) {
    return this.http.post(`${this.URL}/register`, data, { headers: this.getHeaders() });
  }

  changePassword(pass: any) {
    return this.http.put(`${this.URL}/changePassword`, pass , { headers: this.getHeaders() })
  }

  getOne(id: string) {
    let qId = btoa(id);
    return this.http.get(`${this.URL}/usuarioOne/${qId}`, { headers: this.getHeaders() });
  }

  actualizarUser(data: any) {
    return this.http.put(`${this.URL}/usuarioE`, data, { headers: this.getHeaders() });
  }

  eliminarUser(id: string) {
    let qId = btoa(id);
    return this.http.put(`${this.URL}/usuarioDelete/${qId}`, null, { headers: this.getHeaders() });
  }

  habilitarUser(id: string) {
    let qId = btoa(id);
    return this.http.put(`${this.URL}/usuarioEnable/${qId}`, null, { headers: this.getHeaders() });
  }



}
