import { signal } from '@angular/core';
import { TestBed } from '@angular/core/testing';
import { provideHttpClient } from '@angular/common/http';
import { HttpTestingController, provideHttpClientTesting } from '@angular/common/http/testing';
import { ActivatedRoute, convertToParamMap } from '@angular/router';
import { BehaviorSubject } from 'rxjs';
import { Overview } from './overview';
import { TeamStore } from '../../../core/team/team-store';
import { environment } from '../../../../environments/environment';

describe('Overview', () => {
  it('renders balances and the recent team activity', async () => {
    const routeParams = new BehaviorSubject(convertToParamMap({ id: '5' }));
    await TestBed.configureTestingModule({
      imports: [Overview],
      providers: [
        provideHttpClient(),
        provideHttpClientTesting(),
        {
          provide: TeamStore,
          useValue: {
            team: signal({ id: 5, name: 'Team A', alias: 'a', balance: 125, outstanding: 40 }),
            loading: signal(false),
          },
        },
        {
          provide: ActivatedRoute,
          useValue: {
            parent: {
              snapshot: { paramMap: convertToParamMap({ id: '5' }) },
              paramMap: routeParams,
            },
          },
        },
      ],
    }).compileComponents();
    const fixture = TestBed.createComponent(Overview);
    fixture.detectChanges();
    TestBed.inject(HttpTestingController)
      .expectOne(`${environment.apiUrl}teams/5/transactions`)
      .flush([
        {
          id: 1,
          date: '2026-07-31',
          amount: 12,
          type: 'fine',
          note: 'Beitrag',
          playerName: 'Alex',
          isTeamWalletTransaction: false,
        },
      ]);
    await fixture.whenStable();
    fixture.detectChanges();

    expect(fixture.nativeElement.textContent).toContain('125,00');
    expect(fixture.nativeElement.textContent).toContain('Alex');
    expect(fixture.nativeElement.textContent).toContain('Beitrag');
    expect(fixture.nativeElement.textContent).toContain('-12,00');

    routeParams.next(convertToParamMap({ id: '6' }));
    TestBed.inject(HttpTestingController)
      .expectOne(`${environment.apiUrl}teams/6/transactions`)
      .flush([
        {
          id: 2,
          date: '2026-08-01',
          amount: 7,
          type: 'credit',
          note: 'Neues Team',
          playerName: 'Bea',
          isTeamWalletTransaction: false,
        },
      ]);
    fixture.detectChanges();

    expect(fixture.nativeElement.textContent).toContain('Neues Team');
    expect(fixture.nativeElement.textContent).not.toContain('Beitrag');
  });
});
