import { signal } from '@angular/core';
import { TestBed } from '@angular/core/testing';
import { of } from 'rxjs';
import { provideRouter } from '@angular/router';
import { AuthApi } from '../../../../core/auth/auth-api';
import { AuthStore } from '../../../../core/auth/auth-store';
import { TeamStore } from '../../../../core/team/team-store';
import { Invite } from './invite';

describe('Invite', () => {
  it('generates a personalized registration link for an unlinked player', async () => {
    const createInvite = vi.fn(() => of({ token: 'invite-token' }));
    const team = {
      id: 5,
      name: 'Team A',
      alias: 'a',
      balance: 0,
      players: [
        {
          id: 7,
          firstName: 'Bea',
          lastName: 'Test',
          balance: 0,
          active: true,
          teamRole: { id: 1 },
        },
        {
          id: 1,
          firstName: 'Alex',
          lastName: 'Muster',
          balance: 0,
          active: true,
          teamRole: { id: 3 },
          user: { id: 42 },
        },
      ],
    };
    await TestBed.configureTestingModule({
      imports: [Invite],
      providers: [
        provideRouter([]),
        { provide: TeamStore, useValue: { team: signal(team) } },
        { provide: AuthStore, useValue: { currentUser: signal({ id: 42, role: { id: 2 } }) } },
        { provide: AuthApi, useValue: { createInvite } },
      ],
    }).compileComponents();
    const fixture = TestBed.createComponent(Invite);
    fixture.detectChanges();

    fixture.componentInstance['form'].setValue({ playerId: 7 });
    fixture.componentInstance['generateLink']();

    expect(createInvite).toHaveBeenCalledWith({
      teamId: 5,
      teamName: 'Team A',
      playerId: 7,
      playerName: 'Bea Test',
    });
    expect(fixture.componentInstance['inviteLink']()).toContain(
      '/auth/register?token=invite-token',
    );
  });
});
