<?php

declare(strict_types=1);

namespace PpPhp\Core\Designer\Templates;

use PpPhp\Core\Designer\DesignerComponent as C;
use PpPhp\Core\Designer\DesignerTemplate;

/**
 * The login-time two-factor CHALLENGE page (generated/auth/two-factor-verify.php).
 *
 * Mirrors LoginPageTemplate: one `auth_form` component (pageType `two_factor`)
 * carrying the chosen template's look + copy, and `settings.twoFactorTemplate`
 * so the picker gallery highlights the active choice and the build reads it.
 * The four looks are the same as the sign-in templates — Aurora / Halo /
 * Executive / Verdant — so 2FA feels of a piece with login.
 *
 * The in-app 2FA *setup* page (the toggle + method cards) is a separate,
 * built-in page and is not designed here.
 */
final class TwoFactorPageTemplate extends DesignerTemplate
{
    private const VARIANTS = ['aurora', 'halo', 'executive', 'verdant'];

    public function toArray(): array
    {
        $variant = strtolower((string) ($this->options['variant'] ?? 'aurora'));
        if (!in_array($variant, self::VARIANTS, true)) {
            $variant = 'aurora';
        }

        $props = $this->applyBrand(array_replace($this->baseProps(), $this->variantProps($variant)));

        $page = $this->basePage('Two-step verification', [
            C::make('two_factor_auth_form', 'auth_form', '2FA Challenge', $props, [], [], [
                'context' => 'global',
            ], [], [
                'designerRuntime' => 'auth',
                'template' => $variant,
            ]),
        ]);

        $page['pageType'] = 'two_factor';
        $page['table'] = '';
        $page['layout'] = [
            'device' => 'desktop',
            'width' => 'fluid',
            'theme' => 'simple',
            'align' => 'center',
        ];
        $page['settings']['twoFactorTemplate'] = $variant;

        return $page;
    }

    private function baseProps(): array
    {
        return [
            'pageType' => 'two_factor',
            'template' => 'aurora',
            'brandName' => 'PPPHP',
            'brandMark' => 'P',
            'brandTagline' => 'Secure sign-in',
            'title' => 'Enter your verification code',
            'subtitle' => 'Open your authenticator app and type the current 6-digit code to finish signing in.',
            'codeLabel' => '6-digit code',
            'codePlaceholder' => '000000',
            'submitLabel' => 'Verify & sign in',
            // 2FA is a token step — no social entry point, no remember/forgot/signup.
            'socialProviders' => [],
            'showRemember' => false,
            'showForgot' => false,
            'showSignup' => false,
            'showPasswordToggle' => false,
            'showSecurityNote' => true,
            'securityNote' => 'Protected by two-factor authentication',
            'showcaseEyebrow' => 'ONE MORE STEP',
            'showcaseTitle' => 'One more step to keep your account safe.',
            'showcaseText' => 'Enter the code from your authenticator app, email or phone to finish signing in.',
            'testimonial' => '',
            'quoteAuthor' => '',
            'accentColor' => '#6d5dfc',
            'backgroundColor' => '#f4f7fb',
        ];
    }

    private function variantProps(string $variant): array
    {
        return match ($variant) {
            'halo' => [
                'template' => 'halo',
                'brandTagline' => 'Private by design',
                'title' => 'Verify it’s you',
                'subtitle' => 'Enter the current 6-digit code to continue.',
                'securityNote' => 'Two-step verification · SOC 2 ready',
                'showcaseEyebrow' => 'SECURE SIGN-IN',
                'showcaseTitle' => 'A quick check to keep you protected.',
                'showcaseText' => 'Your one-time code confirms it’s really you.',
                'accentColor' => '#7c3aed',
                'backgroundColor' => '#100b2d',
            ],
            'executive' => [
                'template' => 'executive',
                'brandTagline' => 'Operations, secured',
                'title' => 'Two-step verification',
                'subtitle' => 'Enter the 6-digit code to access your workspace.',
                'securityNote' => 'Secure sign-in · Audit logging',
                'showcaseEyebrow' => 'TRUSTED ACCESS',
                'showcaseTitle' => 'Strong verification for critical operations.',
                'showcaseText' => 'A one-time code adds a second layer of protection to every sign-in.',
                'accentColor' => '#0f766e',
                'backgroundColor' => '#eef3f1',
            ],
            'verdant' => [
                'template' => 'verdant',
                'brandTagline' => 'Private and protected',
                'title' => 'Verify your sign-in',
                'subtitle' => 'Enter the current 6-digit code to continue to your account.',
                'securityNote' => 'Private and protected',
                'showcaseEyebrow' => '',
                'showcaseTitle' => '',
                'showcaseText' => '',
                'accentColor' => '#38c96b',
                'backgroundColor' => '#6d9078',
            ],
            default => [],
        };
    }
}
