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

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

  private URL: string = environments.baseUrl;
  private dataUpdated = new Subject<void>();
  dataUpdated$ = this.dataUpdated.asObservable();

  constructor(private http: HttpClient) { }

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

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

  getRegistrosConCalificacion(gradoGrupo: string, materia: string) {
    return this.http.get(`${this.URL}/getAlumnosCalificacion?idGradoGrupo=${gradoGrupo}&idMateria=${materia}`, { headers: this.getHeaders() });
  }

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

  crearRegistro(data: any){
    return this.http.post(`${this.URL}/registrarCalificacion`, data, { headers: this.getHeaders() }).pipe(
      tap(() => this.dataUpdated.next())
    );
  }

  crearRegistroMasivo(data: any){
    return this.http.post(`${this.URL}/registrarCalificacionesMasivas`, data, { headers: this.getHeaders() }).pipe(
      tap(() => this.dataUpdated.next())
    );
  }

  editarRegistro(data: any){
    return this.http.put(`${this.URL}/editarCalificacion`, data, { headers: this.getHeaders() }).pipe(
      tap(() => this.dataUpdated.next())
    );
  }

  eliminarRegistro(data: any) {
    return this.http.put(`${this.URL}/elminarEstadoCalificacion`, data, { headers: this.getHeaders() }).pipe(
      tap(() => this.dataUpdated.next())
    );
  }
  habilitarRegistro(data: any) {
    return this.http.put(`${this.URL}/habilitarEstadoCalificacion`, data, { headers: this.getHeaders() }).pipe(
      tap(() => this.dataUpdated.next())
    );
  }
 
}
