<?php
declare(strict_types=1);

/** 2FA challenge page helpers (QR short link for mobile handoff). */
final class AuthTwoFactorPage {
    /** @return array{short_url?:string,qr_url?:string,long_url:string}|null */
    public static function qrPayload(): ?array {
        $uid = Auth::pending2faUserId();
        if ($uid <= 0) {
            return null;
        }
        $project = Auth::projectBasePath();
        $longUrl = 'https://apps.f0xx.org' . $project . '/?ref=2fa&uid=' . $uid;
        if (!UrlShortenerDatabase::enabled() || !UrlShortenerDatabase::ping()) {
            return ['long_url' => $longUrl];
        }
        $bearers = ShortLinksRepository::listBearers();
        if ($bearers === []) {
            return ['long_url' => $longUrl];
        }
        $bearerId = (int) ($bearers[0]['id'] ?? 0);
        if ($bearerId <= 0) {
            return ['long_url' => $longUrl];
        }
        $mint = ShortLinksRepository::createLink($bearerId, $longUrl, 900);
        if (empty($mint['ok'])) {
            return ['long_url' => $longUrl];
        }
        return [
            'long_url' => $longUrl,
            'short_url' => (string) ($mint['short_url'] ?? ''),
            'qr_url' => (string) ($mint['qr_url'] ?? ''),
        ];
    }
}
