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

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

describe('More', () => {
  it('shows the feature links and logs the user out', async () => {
    const clearSession = vi.fn();
    await TestBed.configureTestingModule({
      imports: [More],
      providers: [
        provideRouter([{ path: 'auth/login', component: LoginStub }]),
        {
          provide: AuthStore,
          useValue: {
            currentUser: signal({ firstName: 'Alex', lastName: 'Muster', email: 'a@b.de' }),
            clearSession,
          },
        },
        { provide: HelpAccessService, useValue: { canOpenGuide: signal(true) } },
        { provide: TeamStore, useValue: { team: signal(null) } },
      ],
    }).compileComponents();
    const fixture = TestBed.createComponent(More);
    fixture.detectChanges();

    expect(fixture.nativeElement.textContent).toContain('Strafenkatalog');
    expect(fixture.nativeElement.textContent).toContain('Einladen');
    expect(fixture.nativeElement.textContent).toContain('Profil');
    expect(fixture.nativeElement.textContent).toContain('Öffentliche Freigabe');
    expect(fixture.nativeElement.textContent).toContain('Anleitung für Verantwortliche');

    expect(fixture.nativeElement.textContent).toContain('Benutzer');
    expect(fixture.nativeElement.querySelector('a[href="/users"]')).not.toBeNull();

    fixture.componentInstance['logout']();
    await fixture.whenStable();
    expect(clearSession).toHaveBeenCalled();
    expect(TestBed.inject(Router).url).toBe('/auth/login');
  });

  it('hides the guide link from regular players', async () => {
    await TestBed.configureTestingModule({
      imports: [More],
      providers: [
        provideRouter([]),
        {
          provide: AuthStore,
          useValue: {
            currentUser: signal({ firstName: 'Alex', lastName: 'Muster', email: 'a@b.de' }),
            clearSession: vi.fn(),
          },
        },
        { provide: HelpAccessService, useValue: { canOpenGuide: signal(false) } },
        { provide: TeamStore, useValue: { team: signal(null) } },
      ],
    }).compileComponents();
    const fixture = TestBed.createComponent(More);
    fixture.detectChanges();

    expect(fixture.nativeElement.textContent).not.toContain('Anleitung für Verantwortliche');
  });

  it('shows the permissions link only to a team manager', async () => {
    const managerTeam = {
      id: 5,
      name: 'Team A',
      alias: 'a',
      balance: 0,
      players: [
        {
          id: 1,
          firstName: 'Alex',
          lastName: 'Muster',
          balance: 0,
          active: true,
          teamRole: { id: 3 },
          user: { id: 42 },
        },
      ],
    };
    await TestBed.configureTestingModule({
      imports: [More],
      providers: [
        provideRouter([]),
        {
          provide: AuthStore,
          useValue: {
            currentUser: signal({
              id: 42,
              firstName: 'Alex',
              lastName: 'Muster',
              email: 'a@b.de',
              role: { id: 2 },
            }),
            clearSession: vi.fn(),
          },
        },
        { provide: HelpAccessService, useValue: { canOpenGuide: signal(false) } },
        { provide: TeamStore, useValue: { team: signal(managerTeam) } },
      ],
    }).compileComponents();
    const fixture = TestBed.createComponent(More);
    fixture.detectChanges();

    expect(fixture.nativeElement.textContent).toContain('Berechtigungen');
  });
});
