import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { environment } from '@msfa-environment';
import { InformativRapportRequest } from '@msfa-models/api/informativ-rapport.request.model';
import { CustomError } from '@msfa-models/error/custom-error';
import { Observable } from 'rxjs';
import { catchError } from 'rxjs/operators';

@Injectable({
  providedIn: 'root',
})
export class InformativRapportApiService {
  private _apiBaseUrl = `${environment.api.url}/rapporter/informativ-rapport`;

  constructor(private httpClient: HttpClient) {}

  public postInformativRapport$(data: InformativRapportRequest): Observable<void> {
    return this.httpClient.post<void>(this._apiBaseUrl, data).pipe(
      catchError((error: Error) => {
        throw new CustomError({
          error,
          message: `Kunde inte spara Informativ rapport.\n\n${error.message}`,
          name: `POST ${this._apiBaseUrl}`,
          data,
          method: 'InformativRapportApiService.postInformativRapport$',
        });
      })
    );
  }
}
