// app/modules/auth/logout/logout.component.ts
import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { AuthService } from '@core/services/auth.service';

@Component({
  template: `
    <div class="logout-container">
      <div class="spinner-border text-primary" role="status">
        <span class="visually-hidden">Déconnexion en cours...</span>
      </div>
      <p class="mt-2">Déconnexion en cours...</p>
    </div>
  `,
  styles: [`
    .logout-container {
      display: flex;
      flex-direction: column;
      align-items: center;
      justify-content: center;
      height: 100vh;
    }
  `]
})
export class Logout implements OnInit {
  
  constructor(
    private authService: AuthService,
    private router: Router
  ) {}

  ngOnInit() {
    this.logout();
  }

  private logout(): void {
    this.authService.logout().subscribe({
      next: () => {
        // Redirection vers la page de login après déconnexion
        this.router.navigate(['/auth/login'], {
          queryParams: { logout: 'success' }
        });
      },
      error: (error) => {
        console.error('Erreur lors de la déconnexion:', error);
        // Rediriger même en cas d'erreur
        this.router.navigate(['/auth/login']);
      }
    });
  }
}