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

@Injectable({
  providedIn: 'root'
})
export class CategoriasMateriasService {

  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}`
    });
  }

  getAllCategorias() {
    return this.http.get(`${this.URL}/categoriaAll`, { headers: this.getHeaders() });
  }
  
  getHabilitados(){
    return this.http.get(`${this.URL}/categoriaHabilitado`, { headers: this.getHeaders() });
  }

  getOne(id: string) {
    
    return this.http.get(`${this.URL}/categoriaOne/${id}`, { headers: this.getHeaders() });
  }

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

  crearCategoria(categoria: any) {
    return this.http.post(`${this.URL}/categoriaSave`, categoria, { headers: this.getHeaders() });
  }

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

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