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 { LogsApi } from './logs-api';

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

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

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

  it('loads logs with page and limit only when no filters are set', () => {
    api.loadLogs({ page: 2, limit: 50 }).subscribe();
    const request = httpMock.expectOne(`${environment.apiUrl}admin/logs?page=2&limit=50`);
    expect(request.request.method).toBe('GET');
    request.flush({ data: [], page: 2, limit: 50, total: 0, hasNextPage: false });
  });

  it('includes level, event, date-range and search filters when set', () => {
    api
      .loadLogs({
        page: 1,
        limit: 50,
        level: 'ERROR',
        event: 'cashbox_export_subscription_run_fail',
        from: '2026-01-01',
        to: '2026-01-31',
        search: 'teamId=5',
      })
      .subscribe();
    const request = httpMock.expectOne(
      `${environment.apiUrl}admin/logs?level=ERROR&event=cashbox_export_subscription_run_fail&from=2026-01-01&to=2026-01-31&search=teamId=5&page=1&limit=50`,
    );
    expect(request.request.method).toBe('GET');
    request.flush({ data: [], page: 1, limit: 50, total: 0, hasNextPage: false });
  });
});
