import { Injectable, OnInit } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { environment } from '../../../environments/environment';

@Injectable({
  providedIn: 'root'
})
export class FriendsService  {

  private apiURL = environment.apiURL;
  constructor(private http: HttpClient) {
   }

  getFriend() {
    console.log("getFriends");
    const url = `${this.apiURL}/friends`;
    const headers = new HttpHeaders({
      'Content-Type': 'application/json',
      Authorization: 'Bearer ' + localStorage.getItem('auth_token'),
    });
    return this.http.get<any[]>(url, { headers });
  }

  getFriendById(id: string) {
    const url = `${this.apiURL}/user/${id}`;
    const headers = new HttpHeaders({
      'Content-Type': 'application/json',
      Authorization: 'Bearer ' + localStorage.getItem('auth_token'),
    });
    return this.http.get<any>(url, { headers });
  }

  acceptFriendById(id: string){
    const url = `${this.apiURL}/friend/${id}/accept`;
    const headers = new HttpHeaders({
      'Content-Type': 'application/json',
      Authorization: 'Bearer ' + localStorage.getItem('auth_token'),
    });
    return this.http.patch<any>(url, [], { headers });
  }

  denyFriendById(id: string){
    const url = `${this.apiURL}/friend/${id}/deny`;
    const headers = new HttpHeaders({
      'Content-Type': 'application/json',
      Authorization: 'Bearer ' + localStorage.getItem('auth_token'),
    });
    return this.http.delete<any>(url, { headers });
  }

  deleteFriend(id: string){
    const url = `${this.apiURL}/friend/${id}/delete`;
    const headers = new HttpHeaders({
      'Content-Type': 'application/json',
      Authorization: 'Bearer ' + localStorage.getItem('auth_token'),
    });
    return this.http.delete<any>(url, { headers });
  }
}
