import { provideHttpClient } from '@angular/common/http';
import { HttpTestingController, provideHttpClientTesting } from '@angular/common/http/testing';
import { TestBed } from '@angular/core/testing';
import { environment } from '../../../environments/environment';
import { NotificationsApi } from './notifications-api';

describe('NotificationsApi', () => {
  let api: NotificationsApi;
  let httpMock: HttpTestingController;

  beforeEach(() => {
    TestBed.configureTestingModule({
      providers: [provideHttpClient(), provideHttpClientTesting()],
    });
    api = TestBed.inject(NotificationsApi);
    httpMock = TestBed.inject(HttpTestingController);
  });

  afterEach(() => httpMock.verify());

  it('loads a page of notifications for a team', () => {
    api.loadNotifications(5, { page: 2, limit: 20 }).subscribe();
    const request = httpMock.expectOne(`${environment.apiUrl}teams/5/notifications?page=2&limit=20`);
    expect(request.request.method).toBe('GET');
    request.flush({ data: [], page: 2, limit: 20, total: 0, hasNextPage: false });
  });

  it('loads the unread count for a team', () => {
    api.loadUnreadCount(5).subscribe();
    const request = httpMock.expectOne(`${environment.apiUrl}teams/5/notifications/unread-count`);
    expect(request.request.method).toBe('GET');
    request.flush({ count: 0 });
  });

  it('marks a single notification as read', () => {
    api.markRead(5, 7).subscribe();
    const request = httpMock.expectOne(`${environment.apiUrl}teams/5/notifications/7/read`);
    expect(request.request.method).toBe('PATCH');
    request.flush(null);
  });

  it('marks all notifications as read', () => {
    api.markAllRead(5).subscribe();
    const request = httpMock.expectOne(`${environment.apiUrl}teams/5/notifications/read-all`);
    expect(request.request.method).toBe('PATCH');
    request.flush(null);
  });
});
