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

@Injectable({
  providedIn: 'root'
})
export class CircularService {
  private URL: string = environments.baseUrl;

  constructor(private http: HttpClient) { }

  private getHeaders(): HttpHeaders {
    const token = localStorage.getItem('token') || '';
    return new HttpHeaders({
      'Content-Type': 'application/json',
      'Authorization': `Bearer ${token}`
    });
  }

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

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

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

  eliminarArchivo(ruta: any) {
    console.log('ruta', ruta);
    return this.http.post(`${this.URL}/eliminarArchivoS3`, ruta, { headers: this.getHeaders() });
  }


  verCircular(data: any) {
    // let qId = btoa(id);
    return this.http.post(`${this.URL}/actualizarVisto`, data, { headers: this.getHeaders() });
  }

  getByUser(id: string) {
    console.log(id)
    return this.http.get(`${this.URL}/getByUserC/${id}`, { headers: this.getHeaders() });
  }

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

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

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

  crearCircular(circular: any) {
    return this.http.post(`${this.URL}/circularSave`, circular, { headers: this.getHeaders() });
  }

  actualizarCircular(circular: any) {
    return this.http.put(`${this.URL}/circularE`, circular, { headers: this.getHeaders() });
  }

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

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

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

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