import { Injectable, inject } from '@angular/core';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';

import { ApiService } from '../api.service';
import { Qcm, QcmCategory, QcmQuestion } from '@models';

@Injectable({ providedIn: 'root' })
export class QcmService {
    private apiService = inject(ApiService);

    private _apiDomain = '/skydive';

    get(type: string): Observable<Qcm> {
        return this.apiService.get(`${this._apiDomain}/qcm/type/${type}`).pipe(map((data) => data.qcm));
    }

    saveChoices(question: object): Observable<QcmQuestion> {
        return this.apiService
            .post(`${this._apiDomain}/qcm/choices`, { question: question })
            .pipe(map((data) => data.question));
    }

    saveQuestions(category: object): Observable<QcmCategory> {
        return this.apiService
            .post(`${this._apiDomain}/qcm/questions`, { category: category })
            .pipe(map((data) => data.category));
    }
}
