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

@Injectable({
  providedIn: 'root'
})
export class GETBREService {
  private _url:string; 
  private _token:string;

  constructor(private _http: HttpClient) {
    this._url = `${apiUriJean}/relacion_tabla`;
    this._token = `Bearer ${localStorage.getItem('token')}`;
  }

  // Consultar las Relaciones de Una Tabla
  public getOneRelationship(codigo_tabla: any){
    return this.getQuery("consulta/" + codigo_tabla).pipe(map((data: any) => data))
  }

  // Obtener Tablas para SELECT de Formulario
  public getRelationshipEsp(codigo_tabla: any){
    return this.getQuery("consulta-unica/" + codigo_tabla).pipe(map((data: any) => data))
  }

  // Relacionar Tablas
  public postRelationship(data: any){
    return this.postQuery("relacionar", data).pipe(map((data: any) => data))
  }

  // Eliminación de Relación
  public deleteRelationship(data: any){ 
    return this.postQuery("elimina", data).pipe(map((data: any) => data))
  }

  private getQuery(query: string) {
    const URL = `${this._url}/${query}`;
    return this._http.get(URL, {
      headers: new HttpHeaders({
        Authorization: this._token
      })
    });
  }

  private postQuery(query: string, body: any) {
    const URL = `${this._url}/${query}`;
    return this._http.post(URL, body, {
      headers: new HttpHeaders({
        Authorization: this._token
      })
    });
  }
}
