import { Component, inject, signal } from '@angular/core';
import { ActivatedRoute, RouterLink } from '@angular/router';
import { MatButtonModule } from '@angular/material/button';
import { MatCardModule } from '@angular/material/card';
import { MatProgressSpinnerModule } from '@angular/material/progress-spinner';
import { AuthApi } from '../../../core/auth/auth-api';

@Component({
  selector: 'app-confirm-email',
  imports: [RouterLink, MatButtonModule, MatCardModule, MatProgressSpinnerModule],
  templateUrl: './confirm-email.html',
  styleUrl: './confirm-email.scss',
})
export class ConfirmEmail {
  private readonly authApi = inject(AuthApi);
  private readonly route = inject(ActivatedRoute);
  private readonly hash = this.route.snapshot.paramMap.get('hash');

  protected readonly confirming = signal(true);
  protected readonly errorMessage = signal<string | null>(null);

  constructor() {
    if (!this.hash) {
      this.confirming.set(false);
      this.errorMessage.set('Der Bestätigungslink ist unvollständig.');
      return;
    }

    this.authApi.confirmEmail(this.hash).subscribe({
      next: () => this.confirming.set(false),
      error: () => {
        this.confirming.set(false);
        this.errorMessage.set('Der Bestätigungslink ist ungültig oder abgelaufen.');
      },
    });
  }
}
