import { TestBed } from '@angular/core/testing';
import { provideHttpClient } from '@angular/common/http';
import { HttpTestingController, provideHttpClientTesting } from '@angular/common/http/testing';
import { TeamStatsApi } from './team-stats-api';
import { environment } from '../../../environments/environment';
import { TeamOverviewStats } from '../../models/team-stats.model';

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

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

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

  it('loads the overview stats for a team', () => {
    const stats: TeamOverviewStats = {
      balanceHistory: [{ month: '2026-07', balance: 125, theoreticalBalance: 150 }],
      monthlyFlow: [{ month: '2026-07', income: 50, expense: 12, penalties: 8 }],
      topOutstanding: [{ playerId: 3, playerName: 'Alex Muster', balance: 20 }],
    };

    api.loadStats(5).subscribe((response) => expect(response).toEqual(stats));

    const request = httpMock.expectOne(`${environment.apiUrl}teams/5/overview/stats`);
    expect(request.request.method).toBe('GET');
    request.flush(stats);
  });
});
