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

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

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

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

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

  EliminarGrupo(grupo: any) {
    return this.http.delete(`${this.URL}/eliminarGrupo`, { headers: this.getHeaders(), params: grupo })
      .pipe(tap(() => this.dataUpdated.next()));
  }

  EliminarPadre(padre: any) {
    return this.http.delete(`${this.URL}/eliminarPadre`, { headers: this.getHeaders(), params: padre })
      .pipe(tap(() => this.dataUpdated.next()));
  }
}
