import { ChangeDetectionStrategy, Component, inject, OnInit } from '@angular/core';
import { UserService } from '../../service/user.service';
import { KeycloakService } from 'keycloak-angular';
import { Router } from '@angular/router';

@Component({
  selector: 'app-login-success',
  standalone: true,
  imports: [],
  templateUrl: './login-success.component.html',
  styleUrl: './login-success.component.css',
  changeDetection: ChangeDetectionStrategy.OnPush,
})
export default class LoginSuccessComponent implements OnInit {
  private userService: UserService = inject(UserService);
  private keycloakService: KeycloakService = inject(KeycloakService);
  private router: Router = inject(Router);

  async ngOnInit() {
    const userProfile = await this.keycloakService.loadUserProfile();
    const user = await this.userService.getOrCreateUser(userProfile);
    sessionStorage.setItem('user', JSON.stringify(user));

    this.router.navigate(['']);
  }
}
