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

@Injectable({
  providedIn: 'root'
})
export class ActividadService {
  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) { }

  getAll() {
    return this.http.get(`${this.URL}/actividades`, { headers: this.getHeaders() });
  }

  getIP() {
    return this.http.get(`${this.URL}/ipLocal`, { headers: this.getHeaders() });
  }

  getPublicIPAddress() {
    return this.http.get<any>('https://api.ipify.org/?format=json').pipe(
      map(response => response.ip),
      catchError(error => {
        console.error('Error al obtener la IP Pública:', error);
        return throwError('Error al obtener la IP Pública');
      })
    );
  }

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

  storeActivity(actividad: ActividadUser) {
    return this.http.post(`${this.URL}/actividad`, actividad, { headers: this.getHeaders() });
  }

}
