import { inject } from '@angular/core';
import { CanActivateFn, Router } from '@angular/router';
import { AuthService } from '../services/auth.service';

export const authGuard: CanActivateFn = (route, state) => {
  const authService = inject(AuthService);
  const router = inject(Router);

    console.log('Guard check for:', state.url, 'isAuthenticated:', authService.isAuthenticated());

  if (authService.isAuthenticated()) {
    return true;
  }

  // Rediriger vers login avec l'URL de retour
  router.navigate(['/auth/sign-in'], { 
    queryParams: { returnUrl: state.url } 
  });
  return false;
};