import { Component, inject, OnInit } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
import { AuthService } from '@service/auth.service';

@Component({
  selector: 'app-verify-email',
  imports: [],
  templateUrl: './verify-email.component.html',
})
export default class VerifyEmailComponent implements OnInit {
  route: ActivatedRoute = inject(ActivatedRoute);
  router: Router = inject(Router);
  authService: AuthService = inject(AuthService);

  ngOnInit(): void {
    const token = this.route.snapshot.queryParamMap.get('email-token');

    if (!token) {
      this.router.navigate(['/']);
      console.log('no token');
      return;
    }

    this.authService.verifyEmail(token).subscribe(() => {
      this.router.navigate(['/'], {
        queryParams: { login: true },
      });
    });
  }
}
