<?php

declare(strict_types=1);

namespace App\Framework\Totp;

/**
 * TOTP QR Code Data Value Object
 *
 * Contains all information needed for QR code generation for TOTP setup.
 */
final readonly class TotpQrData
{
    public function __construct(
        public string $uri,
        public TotpSecret $secret,
        public string $accountName,
        public string $issuer,
        public int $digits,
        public int $period,
        public string $algorithm,
        public ?string $qrCodeSvg = null,
        public ?string $qrCodeDataUri = null
    ) {
    }

    /**
     * Get the otpauth URI
     */
    public function getUri(): string
    {
        return $this->uri;
    }

    /**
     * Get Base32 encoded secret for manual entry
     */
    public function getManualEntryKey(): string
    {
        return $this->secret->toFormattedBase32();
    }

    /**
     * Get setup instructions for manual entry
     */
    public function getManualSetupInstructions(): array
    {
        return [
            'account' => $this->accountName,
            'issuer' => $this->issuer,
            'secret' => $this->getManualEntryKey(),
            'digits' => $this->digits,
            'period' => $this->period,
            'algorithm' => strtoupper($this->algorithm),
        ];
    }

    /**
     * Convert to array for JSON responses
     */
    public function toArray(): array
    {
        return [
            'qr_uri' => $this->uri,
            'qr_code_svg' => $this->qrCodeSvg,
            'qr_code_data_uri' => $this->qrCodeDataUri,
            'manual_entry_key' => $this->getManualEntryKey(),
            'account_name' => $this->accountName,
            'issuer' => $this->issuer,
            'digits' => $this->digits,
            'period' => $this->period,
            'algorithm' => $this->algorithm,
            'setup_instructions' => $this->getManualSetupInstructions(),
        ];
    }
}
