import { Injectable } from '@angular/core'; import { HttpClient } from '@angular/common/http'; import { Observable, BehaviorSubject } from 'rxjs'; import { User } from '../../models/user.model'; @Injectable({ providedIn: 'root' }) export class UserService { private baseUrl = 'http://localhost:8085/api/users'; // Reactive session stream holding the active parsed user object or null if guest private currentSessionSubject = new BehaviorSubject(null); public currentSession$ = this.currentSessionSubject.asObservable(); constructor(private http: HttpClient) { // Automatically initialize stream status from storage on application startup boot phase this.loadInitialSession(); } private loadInitialSession(): void { const activeSessionStr = localStorage.getItem('nexus_mock_session'); if (activeSessionStr) { try { this.currentSessionSubject.next(JSON.parse(activeSessionStr)); } catch (e) { console.error('Initial session recovery failed:', e); } } } // Notifies all reactive stream observers across the system that a new identity is confirmed updateSessionState(sessionData: any): void { localStorage.setItem('nexus_mock_session', JSON.stringify(sessionData)); this.currentSessionSubject.next(sessionData); } // Clear tracking tokens and broadcast the null context downstream to trigger logout transitions clearSessionState(): void { localStorage.removeItem('nexus_mock_session'); this.currentSessionSubject.next(null); } register(userData: User): Observable { return this.http.post(`${this.baseUrl}/register`, userData); } login(credentials: { email: any; password: any }): Observable { return this.http.post(`${this.baseUrl}/login`, { email: credentials.email, password: credentials.password }); } getUserById(userId: number): Observable { return this.http.get(`${this.baseUrl}/${userId}`); } // Initiates Stripe Checkout compiler sequences by fetching the dynamic mapping redirect URL createStripeSession(userId: number, amount: number): Observable<{ url: string }> { return this.http.post<{ url: string }>('http://localhost:8085/api/payment/create-checkout-session', { userId: userId, amount: amount }); } // Requests the operational simulation pipeline wrapper to instantly verify balance records simulateWebhookPayment(userId: number, amount: number): Observable { return this.http.post('http://localhost:8085/api/payment/simulate-success-webhook', { userId: userId, amount: amount }); } }