import { Injectable } from '@angular/core';
import {HttpClient, HttpHeaders} from '@angular/common/http';
import {Observable} from 'rxjs';
import {Comment} from '../models/comment';

@Injectable({
  providedIn: 'root'
})
export class CommentService {
  url: string = 'http://localhost:8080/api/comments';
  constructor(private httpClient: HttpClient) {}

  list(postId: bigint) {
    return this.httpClient.get<Comment[]>(`${this.url}/posts/${postId}`);
  }

  create(content: string, postId: bigint, authorId: string, token: string): Observable<Comment> {
    const httpOptions = {
      headers: new HttpHeaders({
        'Authorization': `Bearer ${token}`
      })
    };
    return this.httpClient.post<Comment>(`${this.url}/posts/${postId}`, {postId: postId, authorId: authorId, content: content}, httpOptions);
  }

  delete(commentId: bigint, token: string) {
    const httpOptions = {
      headers: new HttpHeaders({
        'Authorization': `Bearer ${token}`
      })
    };
    return this.httpClient.delete<Comment>(`${this.url}/${commentId}`, httpOptions);
  }
}
