import { ChangeDetectionStrategy, Component, computed, inject } from '@angular/core';
import { ButtonComponent } from '../../../shared/ui/button/button.component';
import { AuthFacade, Ed25519KeypairService } from '@marketplaces/auth';

/**
 * Ed25519 admin login page. Prepared UI for the flow described in
 * docs/AUTH.md - not linked from any live nav yet, reachable only at
 * `/admin-login` directly, since the backend endpoints it calls do not
 * exist. Once the backend ships, wire `ed25519AuthGuard`/`adminAuthGuard`
 * to redirect here instead of the Telegram dialog.
 */
@Component({
  selector: 'app-admin-login-page',
  standalone: true,
  imports: [ButtonComponent],
  templateUrl: './admin-login-page.component.html',
  styleUrl: './admin-login-page.component.scss',
  changeDetection: ChangeDetectionStrategy.OnPush
})
export class AdminLoginPageComponent {
  private readonly auth = inject(AuthFacade);
  private readonly keypair = inject(Ed25519KeypairService);

  readonly loginPhase = this.auth.loginPhase;
  readonly lastError = this.auth.lastError;
  readonly isEd25519Supported = this.keypair.isSupported();
  readonly isBusy = computed(() => this.loginPhase() !== 'idle' && this.loginPhase() !== 'done');

  readonly phaseLabel = computed(() => {
    switch (this.loginPhase()) {
      case 'requesting-challenge':
        return 'Requesting challenge…';
      case 'signing':
        return 'Signing challenge with device key…';
      case 'verifying':
        return 'Verifying signature…';
      default:
        return '';
    }
  });

  signIn(): void {
    this.auth.login('/backoffice');
  }
}
