import { CommonModule } from '@angular/common';
import { Component } from '@angular/core';
import { ActivatedRoute, Router, RouterModule } from '@angular/router';

import { AuthService } from '../../services/auth.service';
import { UserService } from '../../services/user.service';
import { map2User } from '../../utils/utils';

@Component({
  selector: 'app-login',
  standalone: true,
  imports: [CommonModule, RouterModule],
  template: ``,
})
export class LoginComponent {
  page: string | undefined = this.activatedRoute.snapshot.params['page'] as string | undefined;
  constructor(
    public userService: UserService,
    private activatedRoute: ActivatedRoute,

    private router: Router,
    private authService: AuthService,
  ) {}
  async ngOnInit() {
    const token = await this.authService.getToken();
    const keycloakUser = map2User(token);
    const email = keycloakUser.email;
    const user = await this.userService.getByMail(email);
    // if (!user.subscriptionPlan) {
    //   const subscriptions = await lastValueFrom(this.subscriptionService.getAllSubscriptions(user.email));
    //   const activeSubscription = subscriptions.filter(s => s.status === 'active');
    //   if (activeSubscription.length > 0) {
    //     user.subscriptionPlan = activeSubscription[0].metadata['plan'] === 'Broker Plan' ? 'broker' : 'professional';
    //     this.userService.saveGuaranteed(user);
    //   } else {
    //     this.router.navigate([`/home`]);
    //     return;
    //   }
    // }
    this.router.navigate([`/${this.page}`]);
  }
}
