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

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

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

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

  it('loads a team penalty catalog', () => {
    api.loadPenalties(5).subscribe();
    const request = httpMock.expectOne(`${environment.apiUrl}penalty/5`);
    expect(request.request.method).toBe('GET');
    request.flush([]);
  });

  it('creates a penalty catalog entry', () => {
    const penalty = { teamId: 5, description: 'Zu spät', amount: 5 };
    api.createPenalty(penalty).subscribe();
    const request = httpMock.expectOne(`${environment.apiUrl}penalty`);
    expect(request.request.method).toBe('POST');
    expect(request.request.body).toEqual(penalty);
    request.flush({ id: 1, ...penalty });
  });

  it('updates a penalty catalog entry', () => {
    const update = { description: 'Zu spät', amount: 7.5 };
    api.updatePenalty(8, update).subscribe();
    const request = httpMock.expectOne(`${environment.apiUrl}penalty/8`);
    expect(request.request.method).toBe('PATCH');
    expect(request.request.body).toEqual(update);
    request.flush({ id: 8, ...update });
  });

  it('deletes a penalty catalog entry', () => {
    api.deletePenalty(8).subscribe();
    const request = httpMock.expectOne(`${environment.apiUrl}penalty/8`);
    expect(request.request.method).toBe('DELETE');
    request.flush(null);
  });
});
