import { Component, inject } from '@angular/core';
import { RouterOutlet } from '@angular/router';
import { AuthApi } from './core/auth/auth-api';
import { AuthStore } from './core/auth/auth-store';

@Component({
  selector: 'app-root',
  imports: [RouterOutlet],
  templateUrl: './app.html',
  styleUrl: './app.scss',
})
export class App {
  private readonly authApi = inject(AuthApi);
  private readonly authStore = inject(AuthStore);

  constructor() {
    if (this.authStore.token()) {
      this.authApi.me().subscribe({
        next: (response) => {
          const { token, ...user } = response;
          if (token) {
            this.authStore.setSession(token, user);
          } else {
            this.authStore.updateUser(user);
          }
        },
        error: () => undefined,
      });
    }
  }
}
