<?php

declare(strict_types=1);

namespace PpPhp\Core\Designer\Templates;

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

/**
 * Register page templates. Uses the same coordinated visual shells as Login
 * so a project's login and register pages match,
 * with sign-up copy, name + confirm-password fields, and social sign-up.
 */
final class RegisterPageTemplate 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('register_auth_form', 'auth_form', 'Register Experience', $props, [], [], [
                'onLoad' => 'register:load',
                'server' => 'register:create',
                'context' => 'global',
            ], [], [
                'designerRuntime' => 'auth',
                'template' => $variant,
            ]),
        ]);

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

        return $page;
    }

    private function baseProps(): array
    {
        return [
            'pageType' => 'register',
            'template' => 'aurora',
            'brandName' => 'PPPHP',
            'brandMark' => 'P',
            'brandTagline' => 'Applications, beautifully built.',
            'title' => 'Create your account',
            'subtitle' => 'Start your workspace in less than a minute.',
            'nameLabel' => 'Username',
            'namePlaceholder' => 'Choose a username',
            'emailLabel' => 'Work email',
            'emailPlaceholder' => 'you@example.com',
            'passwordLabel' => 'Password',
            'passwordPlaceholder' => 'At least 6 characters',
            'confirmLabel' => 'Confirm password',
            'confirmPlaceholder' => 'Repeat your password',
            'submitLabel' => 'Create account',
            'signupText' => 'Already have an account?',
            'signupLabel' => 'Sign in',
            'dividerText' => 'or sign up with',
            'socialProviders' => ['google', 'microsoft', 'facebook'],
            'socialPosition' => 'after',
            'socialStyle' => 'buttons',
            'showPasswordToggle' => true,
            'showSignup' => true,
            'showSecurityNote' => true,
            'securityNote' => 'We never share your details with anyone',
            'showcaseEyebrow' => 'START IN MINUTES',
            'showcaseTitle' => 'Join the teams already moving faster.',
            'showcaseText' => 'One account for projects, people, and progress — no setup headaches.',
            'testimonial' => 'Onboarding the whole team took a single afternoon.',
            'quoteAuthor' => '',
            'accentColor' => '#6d5dfc',
            'backgroundColor' => '#f4f7fb',
            'fields' => [
                ['name' => 'username', 'type' => 'text', 'label' => 'Username', 'required' => true],
                ['name' => 'email', 'type' => 'email', 'label' => 'Work email', 'required' => true],
                ['name' => 'password', 'type' => 'password', 'label' => 'Password', 'required' => true],
                ['name' => 'confirm_password', 'type' => 'password', 'label' => 'Confirm password', 'required' => true],
            ],
            'actions' => [
                ['id' => 'register', 'label' => 'Create account', 'variant' => 'primary'],
            ],
            'links' => [
                ['label' => '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' => 'Create your account',
                'subtitle' => 'Create an account or continue with a connected one.',
                'submitLabel' => 'Get started free',
                'socialPosition' => 'before',
                'socialStyle' => 'icons',
                'socialProviders' => ['google', 'microsoft', 'apple', 'github'],
                'securityNote' => 'Private by design · SOC 2 ready',
                'showcaseEyebrow' => 'WELCOME',
                'showcaseTitle' => 'Focus on the work that matters.',
                'showcaseText' => 'A thoughtfully designed workspace for ambitious teams.',
                'testimonial' => '',
                'quoteAuthor' => '',
                'accentColor' => '#7c3aed',
                'backgroundColor' => '#100b2d',
            ],
            'executive' => [
                'template' => 'executive',
                'brandName' => 'PPPHP',
                'brandMark' => 'P',
                'brandTagline' => 'Operations, made clear.',
                'title' => 'Create your account',
                'subtitle' => 'Use your organization account for instant access.',
                'submitLabel' => 'Create account',
                'signupText' => 'Already registered?',
                'dividerText' => 'single sign-on',
                'socialPosition' => 'before',
                'socialStyle' => 'buttons',
                'socialProviders' => ['microsoft', 'google'],
                '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.',
                'testimonial' => 'One secure operating view for the whole organization.',
                'quoteAuthor' => '',
                'accentColor' => '#0f766e',
                'backgroundColor' => '#eef3f1',
            ],
            'verdant' => [
                'template' => 'verdant',
                'brandName' => 'PPPHP',
                'brandMark' => 'P',
                'brandTagline' => 'A clearer path forward.',
                'title' => 'Create your account',
                'subtitle' => 'Join us and begin your next chapter.',
                'submitLabel' => 'Sign up',
                'signupText' => 'Already have an account?',
                'signupLabel' => 'Sign in',
                'socialPosition' => 'after',
                'socialStyle' => 'icons',
                'socialProviders' => ['google', 'microsoft', 'apple', 'facebook'],
                'securityNote' => 'Your details stay private',
                'showcaseEyebrow' => '',
                'showcaseTitle' => '',
                'showcaseText' => '',
                'testimonial' => '',
                'quoteAuthor' => '',
                'accentColor' => '#38c96b',
                'backgroundColor' => '#6d9078',
            ],
            'liquid' => [
                'template' => 'liquid',
                'accentColor' => '#6366f1',
                'backgroundColor' => '#eceef6',
            ],
            'flat' => [
                'template' => 'flat',
                'accentColor' => '#2563eb',
                'backgroundColor' => '#fafafa',
            ],
            default => [],
        };
    }
}
