<?php

declare(strict_types=1);

namespace PpPhp\Core\Designer\Templates;

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

/**
 * Change-password page templates — a `password_form` component rendered
 * inside the application shell (the user is signed in) in one of four
 * layouts:
 *   guard   — centered security card with shield, strength meter, checklist
 *   coach   — password-tips panel beside the form
 *   compact — minimal quick form
 *   verdant — scenic frosted security card
 */
final class ChangePasswordPageTemplate extends DesignerTemplate
{
    private const VARIANTS = ['guard', 'coach', 'compact', 'verdant'];

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

        $props = array_replace($this->baseProps(), $this->variantProps($variant));
        $page = $this->basePage($props['title'], [
            C::make('change_password_form', 'password_form', 'Change Password', $props, [], [], [
                'onLoad' => 'change_password:load',
                'server' => 'change_password:update',
                'context' => 'global',
            ], [], [
                'designerRuntime' => 'auth',
                'template' => $variant,
            ]),
        ]);

        $page['pageType'] = 'change_password';
        $page['table'] = '';
        $page['settings']['changePasswordTemplate'] = $variant;

        return $page;
    }

    private function baseProps(): array
    {
        return [
            'pageType' => 'change_password',
            'template' => 'guard',
            'title' => 'Change password',
            'subtitle' => 'Choose a strong password you have not used before.',
            'accentColor' => '#2563eb',
            'currentLabel' => 'Current password',
            'newLabel' => 'New password',
            'confirmLabel' => 'Confirm new password',
            'submitLabel' => 'Update password',
            'backLabel' => 'Back to profile',
            'minLength' => 8,
            'showStrength' => true,
            'showRequirements' => true,
            'requireUpper' => true,
            'requireNumber' => true,
            'requireSymbol' => false,
            'tipsTitle' => 'Good passwords are…',
            'tipsText' => 'Long over complex — a short phrase beats a cryptic word. Never reuse the password from another site.',
        ];
    }

    private function variantProps(string $variant): array
    {
        return match ($variant) {
            'coach' => [
                'template' => 'coach',
                'subtitle' => 'A minute now saves your account later.',
                'accentColor' => '#0f766e',
            ],
            'compact' => [
                'template' => 'compact',
                'subtitle' => '',
                'showRequirements' => false,
                'requireUpper' => false,
                'requireNumber' => false,
                'minLength' => 6,
            ],
            'verdant' => [
                'template' => 'verdant',
                'title' => 'Secure your account',
                'subtitle' => 'Choose a fresh password to protect your next chapter.',
                'accentColor' => '#38c96b',
                'tipsTitle' => 'Keep it memorable',
                'tipsText' => 'Use a long, unique phrase that you do not use anywhere else.',
            ],
            default => [],
        };
    }
}
