import { inject, Injectable } from '@angular/core';
import { AuthService } from '@service/auth.service';
import { User } from '../model/User';

@Injectable({
  providedIn: 'root',
})
export class UserService {
  private authService = inject(AuthService);

  /**
   * Updates the user's balance locally for immediate UI feedback
   * This should be called before a server-side balance change is made
   * The server update will be reflected when AuthService.loadCurrentUser() is called
   */
  public updateLocalBalance(amount: number): void {
    const currentUser = this.authService.currentUserValue;
    if (currentUser) {
      const updatedUser: User = {
        ...currentUser,
        balance: currentUser.balance + amount,
      };
      this.authService.userSubject.next(updatedUser);
    }
  }

  /**
   * Refreshes the current user's data from the server
   */
  public refreshCurrentUser(): void {
    this.authService.loadCurrentUser();
  }
}
