import { Injectable } from '@nestjs/common';
import { randomBytes, randomUUID } from 'node:crypto';

@Injectable()
export class AdvancedAuthService {
  createWebAuthnRegistrationChallenge(userId: string) {
    return this.challenge(`webauthn-register:${userId}`);
  }

  createWebAuthnLoginChallenge(userId: string) {
    return this.challenge(`webauthn-login:${userId}`);
  }

  createQrSession() {
    return {
      sessionId: randomUUID(),
      status: 'PENDING',
      expiresAt: new Date(Date.now() + 2 * 60_000).toISOString()
    };
  }

  pollQrSession(sessionId: string) {
    return {
      sessionId,
      status: 'PENDING',
      expiresAt: new Date(Date.now() + 2 * 60_000).toISOString()
    };
  }

  private challenge(prefix: string) {
    return {
      challengeId: randomUUID(),
      challenge: `${prefix}:${randomBytes(32).toString('base64url')}`,
      expiresAt: new Date(Date.now() + 5 * 60_000).toISOString()
    };
  }
}
