import { Injectable } from '@angular/core';
import { User } from '../models/user.model';
import { HttpClient, HttpResponse } from '@angular/common/http';
import { Observable } from 'rxjs';
import { NgForm } from '@angular/forms';

@Injectable({
  providedIn: 'root',
})
export class UserService {
  API_URL = 'http://127.0.0.1:3000';

  constructor(private http: HttpClient) {}

  postUser(form: NgForm): Observable<HttpResponse<Response>> {
    let body = {
      login: form.value.login,
      password: form.value.password,
      permissions: 0,
    };

    return this.http.post<any>(`${this.API_URL}/users`, body);
  }

  loginUser(form: NgForm): Observable<HttpResponse<Response>> {
    let body = {
      login: form.value.login,
      password: form.value.password,
    };

    return this.http.post<any>(`${this.API_URL}/users/login`, body);
  }
}

type Response = {
  success: boolean;
};
