<?php

declare(strict_types=1);

namespace PpPhp\Core\Designer\Templates;

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

/**
 * Reset-password page templates. Users land here from the forgot-password
 * email link, so the page reuses the same coordinated visual shells as login
 * and register — the flow stays on-brand end to end.
 */
final class ResetPasswordPageTemplate extends DesignerTemplate
{
    private const VARIANTS = ['liquid', 'flat', 'aurora', 'halo', 'executive', 'verdant'];

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

        $props = $this->applyBrand(array_replace($this->baseProps(), $this->variantProps($variant)));
        $page = $this->basePage($props['title'], [
            C::make('reset_password_auth_form', 'auth_form', 'Reset Password Experience', $props, [], [], [
                'onLoad' => 'reset_password:load',
                'server' => 'reset_password:update',
                'context' => 'global',
            ], [], [
                'designerRuntime' => 'auth',
                'template' => $variant,
            ]),
        ]);

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

        return $page;
    }

    private function baseProps(): array
    {
        return [
            'pageType' => 'reset_password',
            'template' => 'aurora',
            'brandName' => 'PPPHP',
            'brandMark' => 'P',
            'brandTagline' => 'Applications, beautifully built.',
            'title' => 'Set a new password',
            'subtitle' => 'Almost there — choose a strong password to secure your account.',
            'passwordLabel' => 'New password',
            'passwordPlaceholder' => 'At least 8 characters',
            'confirmLabel' => 'Confirm new password',
            'confirmPlaceholder' => 'Repeat your new password',
            'submitLabel' => 'Reset password',
            'signupText' => 'Remembered it?',
            'signupLabel' => 'Back to sign in',
            'socialProviders' => [],
            'showPasswordToggle' => true,
            'showStrength' => true,
            'showSignup' => true,
            'showSecurityNote' => true,
            'securityNote' => 'Reset links expire after one hour',
            'invalidTitle' => 'This link has expired',
            'invalidText' => 'Reset links are valid for one hour. Request a new one and check your inbox.',
            'requestLabel' => 'Request a new link',
            'showcaseEyebrow' => 'ACCOUNT SECURITY',
            'showcaseTitle' => 'A fresh start for your credentials.',
            'showcaseText' => 'Pick something long and memorable — a passphrase beats a puzzle.',
            'testimonial' => '',
            'quoteAuthor' => '',
            'accentColor' => '#6d5dfc',
            'backgroundColor' => '#f4f7fb',
            'fields' => [
                ['name' => 'password', 'type' => 'password', 'label' => 'New password', 'required' => true],
                ['name' => 'confirm_password', 'type' => 'password', 'label' => 'Confirm new password', 'required' => true],
            ],
            'actions' => [
                ['id' => 'reset', 'label' => 'Reset password', 'variant' => 'primary'],
            ],
            'links' => [
                ['label' => 'Back to sign in', 'target' => 'login'],
            ],
            'security' => ['csrf' => true, 'rateLimit' => true, 'guestOnly' => true, 'authRequired' => false],
        ];
    }

    private function variantProps(string $variant): array
    {
        return match ($variant) {
            'halo' => [
                'template' => 'halo',
                'brandName' => 'PPPHP',
                'brandMark' => 'P',
                'brandTagline' => 'Ideas become outcomes.',
                'title' => 'Choose a new password',
                'securityNote' => 'Private by design · SOC 2 ready',
                'showcaseEyebrow' => 'WELCOME BACK',
                'showcaseTitle' => 'Focus on the work that matters.',
                'showcaseText' => 'A thoughtfully designed workspace for ambitious teams.',
                'accentColor' => '#7c3aed',
                'backgroundColor' => '#100b2d',
            ],
            'executive' => [
                'template' => 'executive',
                'brandName' => 'PPPHP',
                'brandMark' => 'P',
                'brandTagline' => 'Operations, made clear.',
                'title' => 'Reset your password',
                'subtitle' => 'Set a new password to regain access to your workspace.',
                'securityNote' => 'Secure SSO · SAML · Audit logging',
                'showcaseEyebrow' => 'TRUSTED OPERATIONS',
                'showcaseTitle' => 'Decisions move faster when the picture is clear.',
                'showcaseText' => 'A secure command center for teams that run critical operations.',
                'accentColor' => '#0f766e',
                'backgroundColor' => '#eef3f1',
            ],
            'verdant' => [
                'template' => 'verdant',
                'brandName' => 'PPPHP',
                'brandMark' => 'P',
                'brandTagline' => 'A clearer path forward.',
                'title' => 'Choose a new password',
                'subtitle' => 'Set a strong new password and continue securely.',
                'submitLabel' => 'Update password',
                'securityNote' => 'Private and protected',
                'showcaseEyebrow' => '',
                'showcaseTitle' => '',
                'showcaseText' => '',
                'accentColor' => '#38c96b',
                'backgroundColor' => '#6d9078',
            ],
            'liquid' => [
                'template' => 'liquid',
                'accentColor' => '#6366f1',
                'backgroundColor' => '#eceef6',
            ],
            'flat' => [
                'template' => 'flat',
                'accentColor' => '#2563eb',
                'backgroundColor' => '#fafafa',
            ],
            default => [],
        };
    }
}
