import { Injectable } from '@angular/core';
import { Router } from '@angular/router';
import { Auth } from '../interface/Auth.interface';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { environments } from '../../../environments/environments';
import { Idle } from '@ng-idle/core';
import { Keepalive } from '@ng-idle/keepalive';

@Injectable({
  providedIn: 'root'
})
export class AuthService {
  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,
    private _router: Router
  ) {

  }

  // función para iniciar sesión

  login(auth: Auth) {
    return this.http.post(`${this.URL}/login`, auth)
  }


  _isLoggedIn(): boolean {
    const token = localStorage.getItem('token');
    return token != null;
  }

  verifyToken(idUsuario: Object) {
    return this.http.post(`${this.URL}/verifyTokens`, idUsuario);
  }

  sendMail(email:any){
    return this.http.post(`${this.URL}/send-test-email`, email)
  }

  //cerrar sesión
  logout() {
    return this.http.get(`${this.URL}/logout`, { headers: this.getHeaders() });
  }




}
