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

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

  public postSignal$(data: SignalRequest): Observable<void> {
    return this.httpClient.post<void>(this._apiBaseUrl, data).pipe(
      catchError((error: Error) => {
        throw new CustomError({
          error,
          message: `Kunde inte spara Signal om arbete eller studier.\n\n${error.message}`,
          name: `POST ${this._apiBaseUrl}`,
          data,
          method: 'SignalApiService.postSignal$',
        });
      })
    );
  }

  constructor(private httpClient: HttpClient) {}
}
