import { ChangeDetectionStrategy, Component, inject, signal } from '@angular/core';
import { CommonModule } from '@angular/common';
import { take } from 'rxjs/operators';
import { VK_ID_GATEWAY } from '../../core/identity/services/vk-id-gateway.token';
import { IconComponent } from '../../shared/ui/icon/icon.component';

/**
 * Standalone VK ID login button, per Sprint 0.1 ("do all after vk" - VK ID
 * is the primary storefront social login going forward, per v3.1 §14).
 * Deliberately not wired into TelegramLoginComponent's dialog yet - that
 * component is the live, working customer/admin login surface, and
 * splicing a second provider into it needs its own careful pass once a
 * real VK OAuth app exists to test against, not a mock-backed bolt-on.
 */
@Component({
  selector: 'app-vk-id-login',
  standalone: true,
  imports: [CommonModule, IconComponent],
  templateUrl: './vk-id-login.component.html',
  styleUrls: ['./vk-id-login.component.scss'],
  changeDetection: ChangeDetectionStrategy.OnPush
})
export class VkIdLoginComponent {
  private readonly gateway = inject(VK_ID_GATEWAY);

  readonly loading = signal(false);

  startLogin(): void {
    this.loading.set(true);
    this.gateway.getAuthorizeUrl().pipe(take(1)).subscribe(url => {
      this.loading.set(false);
      if (typeof window !== 'undefined') {
        window.location.href = url;
      }
    });
  }
}
