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

@Injectable({
  providedIn: 'root'
})
export class CommentService {
  apiUrl: string = '';
  constructor(private httpClient: HttpClient,
              private configurationService: ConfigurationService) {
    this.apiUrl = `${this.configurationService.getApiUrl()}/comments`
  }

  list(postId: bigint) {
    return this.httpClient.get<Comment[]>(`${this.apiUrl}/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.apiUrl}/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.apiUrl}/${commentId}`, httpOptions);
  }
}
