import { Injectable, signal } from '@angular/core';
import { GenericService } from '@common-ui/services/generic.service';
import { IComment } from '@red/domain';
import { firstValueFrom, map } from 'rxjs';

@Injectable({
    providedIn: 'root',
})
export class CommentsApiService extends GenericService<IComment> {
    protected readonly _defaultModelPath = 'manualRedaction';
    readonly comments = signal<Record<string, IComment[]>>({});

    async add(comment: string, annotationId: string, dossierId: string, fileId: string) {
        const url = `${this._defaultModelPath}/comment/add/${dossierId}/${fileId}/${annotationId}`;
        const request = await firstValueFrom(this._post<{ commentId: string }>({ text: comment }, url));
        return request.commentId;
    }

    remove(commentId: string, annotationId: string, dossierId: string, fileId: string) {
        const url = `${this._defaultModelPath}/comment/undo/${dossierId}/${fileId}/${annotationId}/${commentId}`;
        return firstValueFrom(super.delete({}, url));
    }

    fetch(dossierId: string, fileId: string, annotationId: string): Promise<IComment[]> {
        const url = `${this._defaultModelPath}/comments/${dossierId}/${fileId}/${annotationId}`;
        return firstValueFrom(super.getAll<{ comments: IComment[] }>(url).pipe(map(res => res.comments)));
    }
}
