import { Component, signal } from '@angular/core';
import { TestBed } from '@angular/core/testing';
import { provideRouter, Router } from '@angular/router';
import { HelpAccessService } from '../../../../core/help/help-access';
import { TeamStore } from '../../../../core/team/team-store';
import { Guide } from './guide';

@Component({ template: '' })
class EmptyRoute {}

describe('Guide', () => {
  it('renders the handover path and all task-oriented chapters for responsible users', async () => {
    const fixture = await createFixture(true);
    const text = fixture.nativeElement.textContent;

    expect(text).toContain('Neu in der Verantwortung?');
    expect(text).toContain('Amtsübergabe prüfen');
    expect(text).toContain('Buchungen sicher erfassen');
    expect(text).toContain('Verantwortung weitergeben');
    expect(fixture.nativeElement.querySelector('#bookings')).not.toBeNull();
    expect(fixture.nativeElement.querySelector('#faq')).not.toBeNull();
  });

  it('redirects regular players back to the More page after team loading', async () => {
    const fixture = await createFixture(false);
    const router = TestBed.inject(Router);
    await fixture.whenStable();

    expect(router.url).toBe('/team/5/more');
    expect(fixture.nativeElement.textContent).not.toContain('Amtsübergabe prüfen');
  });
});

async function createFixture(canOpen: boolean) {
  await TestBed.configureTestingModule({
    imports: [Guide],
    providers: [
      provideRouter([{ path: 'team/5/more', component: EmptyRoute }]),
      { provide: HelpAccessService, useValue: { canOpenGuide: signal(canOpen) } },
      {
        provide: TeamStore,
        useValue: {
          team: signal({ id: 5 }),
          loading: signal(false),
        },
      },
    ],
  }).compileComponents();
  const fixture = TestBed.createComponent(Guide);
  fixture.detectChanges();
  await fixture.whenStable();
  return fixture;
}
