<?php

declare(strict_types=1);

namespace PpPhp\Core\Designer\Templates;

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

final class LoginPageTemplate 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';
        }

        // Top-level replacement is intentional: provider/field lists belong to a
        // template as a whole and must not retain trailing numeric array entries.
        $props = $this->applyBrand(array_replace($this->baseProps(), $this->variantProps($variant)));
        $props['actions'][0]['label'] = $props['submitLabel'];
        $props['fields'][0]['label'] = $props['emailLabel'];
        $props['fields'][1]['label'] = $props['passwordLabel'];
        $props['fields'][2]['label'] = $props['rememberLabel'];
        $page = $this->basePage('Welcome back', [
            C::make('login_auth_form', 'auth_form', 'Login Experience', $props, [], [], [
                'onLoad' => 'login:load',
                'server' => 'login:authenticate',
                'context' => 'global',
            ], [], [
                'designerRuntime' => 'auth',
                'template' => $variant,
            ]),
        ]);

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

        return $page;
    }

    private function baseProps(): array
    {
        return [
            'pageType' => 'login',
            'template' => 'aurora',
            'brandName' => 'PPPHP',
            'brandMark' => 'P',
            'brandTagline' => 'Applications, beautifully built.',
            'title' => 'Welcome back',
            'subtitle' => 'Sign in to continue to your workspace.',
            'emailLabel' => 'Work email',
            'emailPlaceholder' => 'you@example.com',
            'passwordLabel' => 'Password',
            'passwordPlaceholder' => 'Enter your password',
            'rememberLabel' => 'Keep me signed in',
            'forgotLabel' => 'Forgot password?',
            'submitLabel' => 'Sign in securely',
            'signupText' => 'New here?',
            'signupLabel' => 'Create an account',
            'dividerText' => 'or continue with',
            'socialProviders' => ['google', 'microsoft', 'facebook'],
            'socialPosition' => 'after',
            'socialStyle' => 'buttons',
            'showRemember' => true,
            'showForgot' => true,
            'showSignup' => true,
            'showPasswordToggle' => true,
            'showSecurityNote' => true,
            'securityNote' => 'Protected by enterprise-grade security',
            'showcaseEyebrow' => 'BUILT FOR MOMENTUM',
            'showcaseTitle' => 'Everything your team needs, in one calm place.',
            'showcaseText' => 'Bring projects, people, and progress together — without the usual complexity.',
            'testimonial' => 'Projects, people, and progress, together in one calm place.',
            'quoteAuthor' => '',
            'accentColor' => '#6d5dfc',
            'backgroundColor' => '#f4f7fb',
            'fields' => [
                ['name' => 'email', 'type' => 'email', 'label' => 'Work email', 'required' => true],
                ['name' => 'password', 'type' => 'password', 'label' => 'Password', 'required' => true],
                ['name' => 'remember', 'type' => 'checkbox', 'label' => 'Keep me signed in'],
            ],
            'actions' => [
                ['id' => 'login', 'label' => 'Sign in securely', 'variant' => 'primary'],
            ],
            'links' => [
                ['label' => 'Create an account', 'target' => 'register'],
                ['label' => 'Forgot password?', 'target' => 'forgot_password'],
            ],
            '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' => 'Good to see you',
                'subtitle' => 'Enter your details or use a connected account.',
                'submitLabel' => 'Continue',
                'signupText' => 'First time here?',
                'signupLabel' => 'Start for 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' => 'Sign in',
                'subtitle' => 'Access your organization’s secure workspace.',
                'submitLabel' => 'Sign in',
                'signupText' => 'Need access?',
                'signupLabel' => 'Contact your administrator',
                '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' => 'Welcome back',
                'subtitle' => 'Please sign in to continue to your account.',
                'emailLabel' => 'Username or email',
                'emailPlaceholder' => 'Enter your username',
                'submitLabel' => 'Sign in',
                'signupText' => "Don't have an account?",
                'signupLabel' => 'Sign up',
                'socialPosition' => 'after',
                'socialStyle' => 'icons',
                'socialProviders' => ['google', 'microsoft', 'apple', 'facebook'],
                'securityNote' => 'Private and protected',
                'showcaseEyebrow' => '',
                'showcaseTitle' => '',
                'showcaseText' => '',
                'testimonial' => '',
                'quoteAuthor' => '',
                'accentColor' => '#38c96b',
                'backgroundColor' => '#6d9078',
            ],
            'liquid' => [
                'template' => 'liquid',
                'accentColor' => '#6366f1',
                'backgroundColor' => '#eceef6',
            ],
            'flat' => [
                'template' => 'flat',
                'accentColor' => '#2563eb',
                'backgroundColor' => '#fafafa',
            ],
            default => [],
        };
    }
}
