import { Injectable } from '@angular/core';

import {HttpClient, HttpHeaders} from "@angular/common/http";
import {AuthService} from "@auth0/auth0-angular";
import {tap} from "rxjs/operators";

@Injectable({
  providedIn: 'root'
})
export class RestaurantesService {
  dato:any;

  url='https://api.unified.restaurant/api/v1';


  constructor(private http: HttpClient , public auth: AuthService) { }

  mostrarTodosRes(){
    return this.http.get(`${this.url}/restaurantes`);
  }
   //CRUD DE RESTAURANTE
  agregar(AgreRestaurant:any){
    const headers: HttpHeaders = new HttpHeaders({
      'Content-Type': 'application/json'
    });
    return this.http.post(`${this.url}/restaurantes`, JSON.stringify(AgreRestaurant),{ headers: headers });
  }
  eliminar(codigo:any){
    return this.http.delete(`${this.url}/restaurantes/${codigo}`);
  }
  modificar(codigo:any,ModificarRes:any){
    const headers: HttpHeaders = new HttpHeaders({
      'Content-Type': 'application/json'
    });
    return this.http.put(`${this.url}/restaurantes/${codigo}`, JSON.stringify(ModificarRes),{ headers: headers });
  }


  //CRUD DE USUARIOS
  mostrarUsuarios(){
    return this.http.get(`${this.url}/users`);
  }
  agregarUsu(AgregarUsuario:any){
    const headers: HttpHeaders = new HttpHeaders({
      'Content-Type': 'application/json'
    });
    return this.http.post(`${this.url}/users`, JSON.stringify(AgregarUsuario),{ headers: headers });
  }
  eliminarUsu(codigo:any){
    return this.http.delete(`${this.url}/users/${codigo}`);
  }
  moficiarUsu(codigo:any,Usuariomodificado:any){
    const headers: HttpHeaders = new HttpHeaders({
      'Content-Type': 'application/json'
    });
    return this.http.put(`${this.url}/users/${codigo}`, JSON.stringify(Usuariomodificado), { headers: headers });
  }

  //CRUD DE SECTORES

  mostrarSectores(codigo:any){
    return this.http.get(`${this.url}/${codigo}/sectores`);
  }
  agregarSectores(codigo:any,AgregarSectores:any){
    const headers: HttpHeaders = new HttpHeaders({
      'Content-Type': 'application/json'
    });
    return this.http.post(`${this.url}/${codigo}/sectores`, JSON.stringify(AgregarSectores),{ headers: headers});
  }
  eliminarSec(codigoRes:any,codigoSec:any){
    return this.http.delete(`${this.url}/${codigoRes}/sectores/${codigoSec}`);

  }

  //Loggion user
  Pruebadealgo() {

    this.auth.user$.pipe(user=>this.http.get(`https://api.unified.restaurant/api/v1/users/${user}/restaurantes`)),
      tap((userdato) =>(this.dato = userdato))

    return this.dato;


  }






}
