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 { Profile } from './profile';

describe('Profile', () => {
  it('updates profile names and refreshes the auth store', async () => {
    const updated = {
      id: 42,
      email: 'alex@example.com',
      firstName: 'Alexander',
      lastName: 'Neu',
    };
    const updateProfile = vi.fn(() => of(updated));
    const updateUser = vi.fn();
    await TestBed.configureTestingModule({
      imports: [Profile],
      providers: [
        provideRouter([]),
        { provide: AuthApi, useValue: { updateProfile } },
        {
          provide: AuthStore,
          useValue: {
            currentUser: signal({
              id: 42,
              email: 'alex@example.com',
              firstName: 'Alex',
              lastName: 'Muster',
            }),
            updateUser,
          },
        },
      ],
    }).compileComponents();
    const fixture = TestBed.createComponent(Profile);
    fixture.detectChanges();
    fixture.componentInstance['form'].setValue({
      firstName: 'Alexander',
      lastName: 'Neu',
      oldPassword: '',
      password: '',
    });

    fixture.componentInstance['save']();

    expect(updateProfile).toHaveBeenCalledWith({ firstName: 'Alexander', lastName: 'Neu' });
    expect(updateUser).toHaveBeenCalledWith(updated);
  });
});
