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 MateriaService {

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

  getAllMaterias() {
    return this.http.get(`${this.URL}/materiaAll`, { headers: this.getHeaders() });
  }
  
  getHabilitados(){
    return this.http.get(`${this.URL}/materiaHabilitado`, { headers: this.getHeaders() });
  }

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

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

  crearMateria(materia: any) {
    return this.http.post(`${this.URL}/materiaSave`, materia, { headers: this.getHeaders() });
  }

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

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

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

  crearAnuncio(anuncio: any) {
    return this.http.post(`${this.URL}/crearAnuncio`, anuncio, { headers: this.getHeaders() });
  }

  editarAnuncio(anuncio:any){
    return this.http.put(`${this.URL}/anuncioE`, anuncio, { headers: this.getHeaders() });
  }

  eliminarAnuncio(id: number) {

    return this.http.put(`${this.URL}/anuncioEliminar/${id}`, null, { headers: this.getHeaders() });
  }

}
