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

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

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

  public getTables() {
    return this.getQuery('consulta').pipe(map((data: any) => data));
  }

  public registerTable(table: any) {
    return this.postQuery('registra', table).pipe(map((data: any) => data));
  }

  public updateStateTable(table: any) {
    return this.postQuery('modifica-state', table).pipe(map((data: any) => data));
  }

  public updateTable(obj_table: any) {
    return this.postQuery('modifica', obj_table).pipe(map((data: any) => data));
  }


  // Obtener Columnas de Tablas
  public getColumns(data: any) {
    return this.postQuery('consulta-columnas', data).pipe(
      map((data: any) => data)
    );
  }
  // Obtener Columnas de Tablas
  public getColumnsExtra(data: any) {
    return this.postQuery('consulta-columnas-extra', 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,
      }),
    });
  }

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