import { HttpClient, HttpParams } from '@angular/common/http';
import { Injectable, inject } from '@angular/core';
import { Observable } from 'rxjs';
import { environment } from '../../../environments/environment';
import { NotificationPage, NotificationQuery } from '../../models/notification.model';

@Injectable({ providedIn: 'root' })
export class NotificationsApi {
  private readonly http = inject(HttpClient);

  loadNotifications(teamId: number, query: NotificationQuery): Observable<NotificationPage> {
    const params = new HttpParams().set('page', query.page).set('limit', query.limit);
    return this.http.get<NotificationPage>(`${environment.apiUrl}teams/${teamId}/notifications`, {
      params,
    });
  }

  loadUnreadCount(teamId: number): Observable<{ count: number }> {
    return this.http.get<{ count: number }>(
      `${environment.apiUrl}teams/${teamId}/notifications/unread-count`,
    );
  }

  markRead(teamId: number, id: number): Observable<void> {
    return this.http.patch<void>(`${environment.apiUrl}teams/${teamId}/notifications/${id}/read`, {});
  }

  markAllRead(teamId: number): Observable<void> {
    return this.http.patch<void>(`${environment.apiUrl}teams/${teamId}/notifications/read-all`, {});
  }
}
