import { Injectable } from '@angular/core';
import { Activity, mapResponseToActivity } from '@msfa-models/activity.model';
import { PeriodiskRedovisningRequest } from '@msfa-models/api/periodisk-redovisning.request.model';
import { DeltagareAvrop } from '@msfa-models/avrop.model';
import { DeltagareApiService } from '@msfa-services/api/deltagare.api.service';
import { GemensamPlaneringApiService } from '@msfa-services/api/gemensam-planering.api.service';
import { PeriodiskRedovisningApiService } from '@msfa-services/api/periodisk-redovisning.api.service';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';

@Injectable()
export class PeriodiskRedovisningFormService {
  constructor(
    private periodiskRedovisningApiService: PeriodiskRedovisningApiService,
    private deltagareApiService: DeltagareApiService,
    private gemensamPlaneringApiService: GemensamPlaneringApiService
  ) {}

  fetchAvropInformation$(genomforandeReferens: number): Observable<DeltagareAvrop> {
    return this.deltagareApiService.fetchAvropInformation$(genomforandeReferens);
  }

  submitPeriodiskRedovisning$(submitData: PeriodiskRedovisningRequest): Observable<void> {
    return this.periodiskRedovisningApiService.postPeriodiskRedovisning$(submitData);
  }

  getAllChosenActivities(genomforandeReferens: number): Observable<Activity[]> {
    return this.gemensamPlaneringApiService
      .fetchAllChosenActivities$(genomforandeReferens)
      .pipe(map(({ data }) => data.map(activity => mapResponseToActivity(activity))));
  }
}
